Path takes a path-like string and adjusts everything for the current OS, either Windows or Linux. For example, on Linux it would convert all backslashes to forward slashes, and on Windows it would do the reverse. Full article: Python 3 Quick Tip: The easy way to deal with file paths on Windows, Mac and Linux.
Steps on WindowsOpen Search and Type Edit the System Environment Variables. Then Click on the "Environment Variables" Button in the Down Corner. There you can see all of the paths associated to where python, pip and other binaries are located that you call on command line.
On Windows, paths are written using backslashes (\) as the separator between folder names. OS X and Linux, however, use the forward slash (/) as their path separator. If you want your programs to work on all operating systems, you will have to write your Python scripts to handle both cases.
What is PYTHONPATH? PYTHONPATH is an environment variable which the user can set to add additional directories that the user wants Python to add to the sys. path directory list. In short, we can say that it is an environment variable that you set before running the Python interpreter.
you can use always:
'C:/mydir'
this works both in linux and windows. Other posibility is
'C:\\mydir'
if you have problems with some names you can also try raw string literals:
r'C:\mydir'
however best practice is to use the os.path
module functions that always select the correct configuration for your OS:
os.path.join(mydir, myfile)
From python 3.4 you can also use the pathlib module. This is equivelent 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"
Yes, \
in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence \a
, which is collapsed into one character that is ASCII Bell:
>>> '\a'
'\x07'
>>> len('\a')
1
>>> 'C:\meshes\as'
'C:\\meshes\x07s'
>>> print('C:\meshes\as')
C:\meshess
Other common escape sequences include \t
(tab), \n
(line feed), \r
(carriage return):
>>> list('C:\test')
['C', ':', '\t', 'e', 's', 't']
>>> list('C:\nest')
['C', ':', '\n', 'e', 's', 't']
>>> list('C:\rest')
['C', ':', '\r', 'e', 's', 't']
As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python's escape sequences is here.
There are a variety of ways to deal with that:
Python will not process escape sequences in string literals prefixed with r
or R
:
>>> r'C:\meshes\as'
'C:\\meshes\\as'
>>> print(r'C:\meshes\as')
C:\meshes\as
Python on Windows should handle forward slashes, too.
You could use os.path.join
...
>>> import os
>>> os.path.join('C:', os.sep, 'meshes', 'as')
'C:\\meshes\\as'
... or the newer pathlib
module
>>> from pathlib import Path
>>> Path('C:', '/', 'meshes', 'as')
WindowsPath('C:/meshes/as')
Use Path
:
from pathlib import Path
data_folder = Path("source_data/text_files/")
file_to_open = data_folder / "raw_data.txt"
print(file_to_open.read_text())
Path
takes a path-like string and adjusts everything for the current OS, either Windows or Linux. For example, on Linux it would convert all backslashes to forward slashes, and on Windows it would do the reverse.
Full article: Python 3 Quick Tip: The easy way to deal with file paths on Windows, Mac and Linux
My experience:
os.path.join(...)
, then switched to normpath(...)
then finally switched to Path(...)
. Having used all three, Path is the best of all worlds.Advantages of Path over os.path.join(...)
:
/
(see above).Advantages of path over normpath(...)
:
/
rather than having to fall back to os.path.join(...)
, with nested normpath calls to fix things up.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With