You can use always:
'C:/mydir'
This works both in Linux and Windows.
Another possibility is:
'C:\\mydir'
If you have problems with some names you can also try raw string literals:
r'C:\mydir'
However, the best practice is to use the os.path module functions that always joins with the correct path separator (os.path.sep) for your OS:
os.path.join(mydir, myfile)
From python 3.4 you can also use the pathlib module. This is equivalent to the above:
pathlib.Path(mydir, myfile)
or:
pathlib.Path(mydir) / myfile
Answer from joaquin on Stack OverflowYou can use always:
'C:/mydir'
This works both in Linux and Windows.
Another possibility is:
'C:\\mydir'
If you have problems with some names you can also try raw string literals:
r'C:\mydir'
However, the best practice is to use the os.path module functions that always joins with the correct path separator (os.path.sep) for your OS:
os.path.join(mydir, myfile)
From python 3.4 you can also use the pathlib module. This is equivalent to the above:
pathlib.Path(mydir, myfile)
or:
pathlib.Path(mydir) / myfile
Use the os.path module.
os.path.join( "C:", "meshes", "as" )
Or use raw strings
r"C:\meshes\as"
I would also recommend no spaces in the path or file names. And you could use double backslashes in your strings.
"C:\\meshes\\as.jpg"
Python and the PATH variable in Windows 10
python - How to add to the PYTHONPATH in Windows, so it finds my modules/packages? - Stack Overflow
Windows path to Python path
Python Installation
Videos
For Windows 10/8/7:
- Open
System Properties(Right clickComputerin the start menu, or use the keyboard shortcut Win+Pause) - Click
Advanced system settingsin the sidebar. - Click
Environment Variables... - Select
PATHin theSystem variablessection - Click
Edit Add Python's path to the end of the list (the paths are separated by semicolons). For example:
C:\Windows;C:\Windows\System32;C:\Python27
For Windows XP:
- Open
System Properties(Type it in the start menu, or use the keyboard shortcut Win+Pause) - Switch to the
Advancedtab - Click
Environment Variables... - Select
PATHin theSystem variablessection - Click
Edit Add Python's path to the end of the list (the paths are separated by semicolons). For example:
C:\Windows;C:\Windows\System32;C:\Python27Test on a new terminal window or if using an integrated terminal within a text editor, close and restart your editor or the changes won't be applied.
For anyone trying to achieve this with Python 3.3+, the Windows installer now includes an option to add python.exe to the system search path. Read more in the docs.
Do you remember checking the box in the image shown below to add your Python installation to PATH in Windows 10?
Adding Python to PATHWell, I was curious! What if someone accidentally forgot to check that little radio button? What would be the possible recovery steps?
So I spent my evening yesterday uninstalling Python and re-installing to figure out a way to add Python to the PATH post-installation.
If you forget to add Python to the PATH variable, you won't be able to run Python in your command line.
You will get a message saying,
‘python' is not recognized as an internal or external command.
Here are the steps I used to add Python to the PATH variable in Windows 10.
-
Copy the directory to which Python is being installed.
-
Search for System Properties in the Search Bar.
-
Click on the "Environment Variables" button in System Properties.
-
Click on the "Path" variable and then click on "Edit".
-
Now here's the deal. Paste the directory path from Step 1 at the top of the list. You can use the "Move up" button to get it to the top. Now click on "Ok".
I found out that if you paste the path at the end, it still doesn't work. Can someone tell me why?
Anyways, if the steps are not clear to anyone, here is a visual guide containing images of every step, on my blog for achieving the above objective. If you are interested, do take a look.
You know what has worked for me really well on windows.
My Computer > Properties > Advanced System Settings > Environment Variables >
Just add the path as C:\Python27 (or wherever you installed python)
OR
Then under system variables I create a new Variable called PythonPath. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

This is the best way that has worked for me which I hadn't found in any of the docs offered.
EDIT: For those who are not able to get it, Please add
C:\Python27;
along with it. Else it will never work.
Windows 7 Professional I Modified @mongoose_za's answer to make it easier to change the python version:
- [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
- Click [New] under "System Variable"
- Variable Name: PY_HOME, Variable Value:C:\path\to\python\version

- Click [OK]
- Locate the "Path" System variable and click [Edit]
Add the following to the existing variable:
%PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;

Click [OK] to close all of the windows.
As a final sanity check open a command prompt and enter python. You should see
>python [whatever version you are using]
If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.
I have been searching the web for a solution without a result.
I made this small script that can be used. Feel free to use it.
I wonder is there a better solution on the issue to convert windows path to python path?
"""
A path, in Windows "C:\Folder" . in Python it is "C:/Folder".
In Python the "\" character can get interpreted as the ESCAPE character.
When an 'r' or 'R' prefix is present, a character following a backslash is
included in the string without change, and all backslashes are left in the string.
"""
def main():
# - YOUR INPUT - IMPORTANT: keep the r in front of path string here.
win_path = r"<TYPE YOUR PATH HERE>"
# Call function to convert from windows to Python path.
path_py = py_path(win_path)
print("Your Python Path: ", path_py)
def py_path(win_path):
python_path = "" # The result of this script.
# Convert to ASCII list
ascii_values_list = []
for character in win_path:
ascii_values_list.append(ord(character))
# Replace all ASCII values for "\" (=92) with value for "/" (=47).
for i in range(0, len(ascii_values_list)):
if ascii_values_list[i] == 92:
ascii_values_list[i] = 47
path_py = "" # Convert ASCII list to string
for val in ascii_values_list:
path_py = path_py + chr(val)
if path_py[-1] != "/": # Add "/" at end of path if needed.
path_py = path_py + "/"
return path_py
if __name__ == "__main__":
main() # Script goes there.
# EOF