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"
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