Doubled backslashes in Windows paths

Peter Otten __peter__ at web.de
Fri Oct 7 05:27:00 EDT 2016


Oz-in-DFW wrote:

> I'm using Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC
> v.1900 32 bit (Intel)] on Windows 7
> 
> I'm trying to write some file processing that looks at file size,
> extensions, and several other things and I'm having trouble getting a
> reliably usable path to files.
> 
> The problem *seems* to be doubled backslashes in the path, but I've read
> elsewhere that this is just an artifact of the way the interpreter
> displays the strings.
> 
> I'm getting an error message on an os.path.getsize call;
> 
>     Path: -
>     "C:\Users\Rich\Desktop\2B_Proc\2307e60da6451986dd8d23635b845386.jpg" -
>     Traceback (most recent call last):
>       File "C:\Users\Rich\workspace\PyTest\test.py", line 19, in <module>
>         if os.path.getsize(path)>10000:
>       File "C:\Python32\lib\genericpath.py", line 49, in getsize
>         return os.stat(filename).st_size
>     WindowsError: [Error 123] The filename, directory name, or volume
>     label syntax is incorrect:
>     '"C:
\\Users\\Rich\\Desktop\\2B_Proc\\2307e60da6451986dd8d23635b845386.jpg"'
> 
> From (snippet)
> 
>                 path = '"'+dirpath+name+'"'
>                 path = os.path.normpath(path)
>                 print("Path: -",path,"-")
>                 if os.path.getsize(path)>10000:
>                     print("Path: ",path," Size:
>     ",os.path.getsize(dirpath+name))
> 
> but if I manually use a console window and cut and paste the path I
> print, it works;
> 
>     C:\>dir
>     "C:\Users\Rich\Desktop\2B_Proc\2307e60da6451986dd8d23635b845386.jpg"
>      Volume in drive C is Windows7_OS
> 
>      Directory of C:\Users\Rich\Desktop\2B_Proc
> 
>     10/03/2016  08:35 AM            59,200
>     2307e60da6451986dd8d23635b845386.jpg
>                    1 File(s)         59,200 bytes
>                    0 Dir(s)  115,857,260,544 bytes free
> 
> So the file is there and the path is correct. I'm adding quotes to the
> path to deal with directories and filenames that have spaces in them.
> If I drop the quotes, everything works as I expect *until* I encounter
> the first file/path with spaces.

You have to omit the extra quotes. That the non-working path has spaces in 
it is probably not the cause of the problem. If a string *literal* is used, 
e. g. "C:\Path\test file.txt" there may be combinations of the backslash and 
the character that follows that are interpreted specially -- in the example 
\P is just \ followed by a P whereas \t is a TAB (chr(9)):

>>> print("C:\Path\test file.txt")
C:\Path est file.txt

To fix the problem either use forward slashes (which are understood by 
Windows, too) or or duplicate all backslashes, 

>>> print("C:\\Path\\test file.txt")
C:\Path\test file.txt

or try the r (for "raw string") prefix:

>>> print(r"C:\Path\test file.txt")
C:\Path\test file.txt

This applies only to string literals in Python source code; if you read a 
filename from a file or get it as user input there is no need to process the 
string:

>>> input("Enter a file name: ")
Enter a file name: C:\Path\test file.txt
'C:\\Path\\test file.txt'

> I'll happily RTFM, but I need some hints as to which FM to R
> 

https://docs.python.org/3.5/tutorial/introduction.html#strings
https://docs.python.org/3.5/reference/lexical_analysis.html#string-and-bytes-literals

PS: Unrelated, but a portable way to combine paths is os.path.join() which 
will also insert the platform specific directory separator if necessary:

>>> print(os.path.join("foo", "bar\\", "baz"))
foo\bar\baz





More information about the Python-list mailing list