Python Forum

Full Version: How to print a File Path ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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\Dataset
Never 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
You can use / as a path separator everywhere. Python will deal with it according to the OS on which is running.
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