Open File in Another Directory (Python)

后端 未结 4 866
抹茶落季
抹茶落季 2020-12-08 02:56

I\'ve always been sort of confused on the subject of directory traversal in Python, and have a situation I\'m curious about: I have a file that I want to access in a directo

4条回答
  •  无人及你
    2020-12-08 03:35

    Its a very old question but I think it will help newbies line me who are learning python. If you have Python 3.4 or above, the pathlib library comes with the default distribution.

    To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest. To indicate that the path is a raw string, put r in front of the string with your actual path.

    For example,

    from pathlib import Path
    
    dataFolder = Path(r'D:\Desktop dump\example.txt')
    

    Source: The easy way to deal with file paths on Windows, Mac and Linux

    (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

提交回复
热议问题