How should I write a Windows path in a Python string literal?
Asked Answered
C

5

284

Suppose I need to refer to the path C:\meshes\as. If I try writing that directly, like "C:\meshes\as", I encounter problems - either some exception, or the path just doesn't work. Is this because \ is acting as an escape character? How should I write the paths?

Centro answered 1/6, 2010 at 22:29 Comment(1)
See also: medium.com/swlh/…Keeper
C
416

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
Connors answered 1/6, 2010 at 22:30 Comment(9)
@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.Connors
The only thing to be careful with on raw strings is that they can't end with \Seitz
You can use os.path.join() to remove the need to end paths with \.Intrench
I 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 slashesKoller
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:Capriola
import re; re.sub(r'\\', r'/', test)Masonmasonic
"this works both in linux and windows" - well, the forward slashes work, but drive letters are nonsense in linux :)Eleph
Yeah but how do you solve a white space in the folder name?Byington
@Byington what kind of problems are you having with white space in a folder name? Might be a problem on a command line, but not in code.Ward
B
46

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"
Bayonet answered 1/6, 2010 at 22:30 Comment(5)
os.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.)Raymund
@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?Jojo
@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.Freda
but it doesn't work either. What works is os.path.join( "C:\\", "meshes", "as" )Dukes
@JackO'Connor you're dealing with Windows and you're worrying about cleanliness? ;-)Inhuman
H
37

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:

  1. 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
    
  2. Python on Windows should handle forward slashes, too.

  3. You could use os.path.join ...

    >>> import os
    >>> os.path.join('C:', os.sep, 'meshes', 'as')
    'C:\\meshes\\as'
    
  4. ... or the newer pathlib module

    >>> from pathlib import Path
    >>> Path('C:', '/', 'meshes', 'as')
    WindowsPath('C:/meshes/as')
    
Howarth answered 31/3, 2018 at 12:31 Comment(1)
"Other common escape sequences include" - don't forget \U used for 32-bit Unicode code point escapes. Windows users are very commonly tripped up by this, because they want to access files within C:\Users.Eleph
J
19

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 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(...):

  • 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 to os.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.
Jacques answered 3/7, 2021 at 8:26 Comment(0)
N
8

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')
Neusatz answered 28/7, 2021 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.