How to print a File Path ? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to print a File Path ? (/thread-13018.html) |
How to print a File Path ? - Astrikor - Sep-24-2018 Hello I am sure I must have a misunderstanding about Python codec formats, so I am looking for advice here. I want to print a file path: print("C:\User\Astrikor\Dataset")I get the following error message: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXXD escape What's wrong ?? Thanks Astrikor RE: How to print a File Path ? - snippsat - Sep-24-2018 The okay ways. >>> print(r"C:\User\Astrikor\Dataset") C:\User\Astrikor\Dataset >>> print("C:/User/Astrikor/Dataset") C:/User/Astrikor/Dataset >>> print("C:\\User\\Astrikor\\Dataset") C:\User\Astrikor\DatasetNever a singel \ in path for Window,because of escape character.In case over the problem is \U which gives unicodeescape error.A other example will never find top folder,because \t Horizontal Tab(TAB).>>> s = 'C:\top' >>> s 'C:\top' >>> print(s) C: op >>> >>> # Fix >>> s = r'C:\top' >>> s 'C:\\top' >>> print(s) C:\top RE: How to print a File Path ? - wavic - Sep-24-2018 You can use / as a path separator everywhere. Python will deal with it according to the OS on which is running. RE: How to print a File Path ? - Astrikor - Sep-24-2018 Thanks to both. I also wanted to insert a variable file name into the path and your two replies have fixed it. Problem sorted! With blessings, Astrikor |