What is the best way to represent a Windows directory, for example "C:\meshes\as"? I have been trying to modify a script but it never works because I can't seem to get the directory right, I assume because of the '\' acting as escape character?
-
See also: https://medium.com/swlh/solved-windows-pip-command-not-found-or-pip-is-not-recognized-as-an-internal-or-external-command-dd34f8b2938f – Server Overflow Mar 21 '22 at 10:47
6 Answers
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
-
2@Gareth, I am very lazy and often found myself using '/'. However in the long run the use of os.path is more convenient. It also allows you to use mydir and myfile as variables that you can easily modify. – joaquin Jun 01 '10 at 22:48
-
22The only thing to be careful with on raw strings is that they can't end with \ – Douglas Leeder Jun 29 '10 at 16:15
-
1You can use os.path.join() to remove the need to end paths with \. – Will Ediger Aug 05 '14 at 14:53
-
1I like the r (raw string) syntax. Useful if you're copying a long path where you'd usually have to replace all the backslashes with forward slashes – peterb Aug 21 '16 at 05:07
-
raw string can end with \\ so we can concatenate a file to the path: codecs.open(r"C:\maXbox\EKON24\tweet_data\\" + file, 'r', encoding='utf-8') as f: – Max Kleiner Jun 24 '20 at 13:34
-
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"
-
14os.path.join may not behave as you expect when a component is a drive letter, since relative paths are allowed even then. (The result of the first line is 'C:meshes\\as' on Windows.) – dash-tom-bang Jun 01 '10 at 23:04
-
@dash-tom-bang's comment is really important. Is the right thing to do to put `"C:\"` as the first entry? Does that mess up some of cleanliness of using `join`? – Jack O'Connor Feb 21 '14 at 00:53
-
1@JackO'Connor that's what I do. You certainly do not want to put `"C:\"` in the middle of the file name. Besides, you can use `os.path.normpath` before or after a join, to make sure the path gets printed nicely. – Agostino Apr 07 '15 at 18:03
-
2but it doesn't work either. What works is `os.path.join( "C:\\", "meshes", "as" )` – Jean-François Fabre Mar 02 '19 at 11:32
-
@JackO'Connor you're dealing with Windows and you're worrying about cleanliness? ;-) – Jürgen A. Erhard Aug 08 '21 at 09:36
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
rorR:>>> r'C:\meshes\as' 'C:\\meshes\\as' >>> print(r'C:\meshes\as') C:\meshes\asPython 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
pathlibmodule>>> from pathlib import Path >>> Path('C:', '/', 'meshes', 'as') WindowsPath('C:/meshes/as')
- 40,483
- 12
- 109
- 137
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:
- I spent 6 months using
os.path.join(...), then switched tonormpath(...)then finally switched toPath(...). Having used all three, Path is the best of all worlds.
Advantages of Path over os.path.join(...):
- Cleaner.
- Less typing.
- Easier to read the paths (i.e. more readable).
- Can join two different paths using
/(see above). - More modern.
Advantages of path over normpath(...):
- Can join paths using
/rather than having to fall back toos.path.join(...), with nested normpath calls to fix things up. - Cleaner.
- Less typing.
- Easier to read the paths (i.e. more readable).
- Less chance of bugs when porting between Linux and Windows.
- More modern.
- 71,009
- 55
- 247
- 290
Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python raw string treats backslash () as a literal character. This is useful when we want to have a string that contains backslash and don’t want it to be treated as an escape character.
Doing Manually Such as:
WindowsPath("C:\meshes\as")
or by using r or R:
WindowsPath(r'C:/meshes/as')
- 302
- 3
- 11
Use PowerShell
In Windows, you can use / in your path just like Linux or macOS in all places as long as you use PowerShell as your command-line interface. It comes pre-installed on Windows and it supports many Linux commands like ls command.
If you use Windows Command Prompt (the one that appears when you type cmd in Windows Start Menu), you need to specify paths with \ just inside it. You can use / paths in all other places (code editor, Python interactive mode, etc.).
- 3,776
- 4
- 28
- 41