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"
Total Noob - How do I properly refer to a file path in python?
Path in python
Best Python Learning Path
Hello,
I am trying to write in the path "c:/gpt-2-2/models" into a python code. When I run the code I get a syntax error at the "C:" part. I guess I don't know how to properly write file paths in python. Any help would be appreciated.
Can anyone explain why and how we can give path to python. So that our code works.And what pip actually does.