Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print a File Path ?
#1
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
Reply
#2
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
Reply
#3
You can use / as a path separator everywhere. Python will deal with it according to the OS on which is running.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  import a function from another file using relative path paul18fr 6 3,644 Aug-01-2024, 06:40 AM
Last Post: paul18fr
  Cannot get cmd to print Python file Schauster 11 3,466 May-16-2024, 04:40 PM
Last Post: xMaxrayx
  File path by adding various variables Mishal0488 2 4,292 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Script File Failure-Path Error? jerryf 13 8,459 Nov-30-2022, 09:58 AM
Last Post: jerryf
  Saving the print result in a text file Calli 8 4,440 Sep-25-2022, 06:38 PM
Last Post: snippsat
  failing to print not matched lines from second file tester_V 14 9,291 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,745 Mar-30-2022, 04:14 AM
Last Post: DaveG
Sad Want to Save Print output in csv file Rasedul 5 15,963 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Convert legacy print file to XLSX file davidm 1 2,601 Oct-17-2021, 05:08 AM
Last Post: davidm
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 3,297 Sep-09-2021, 01:25 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020