Error with double backslash in Windows path in Python

后端 未结 2 552
清歌不尽
清歌不尽 2020-12-11 06:06

I want to work with paths in Windows in Python 3.3, but I have an error:

FileNotFoundError: [Errno 2] No such file or directory: \'E:\\\\dir\\\\.

相关标签:
2条回答
  • 2020-12-11 06:39

    Changing '\\' for '/' worked for me. I created a directory named 'a' in C:/ for this example.

    >>> (Python interpreter)
    >>> import os
    >>> os.path.isdir('C:/a/)')
    >>> True
    >>> os.path.isfile('C:/a/)')
    >>> False
    
    0 讨论(0)
  • 2020-12-11 06:47

    If you are using a raw-string, then you do not escape backslashes:

    f(r'E:\dir')
    

    Of course, this problem (and many others like it) can be solved by simply using forwardslashes in paths:

    f('E:/dir')
    
    0 讨论(0)
提交回复
热议问题