Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python raw string help
#1
Hi Team,

Can we use r as raw string for variable folder_path.
because user is passing folderpath via command line.



Path(r'E:\data\transport_data') I understood this line of code.

Path(r{source_path}) .......... can we use r word infront of source_path


from pathlib import Path

#1)  r string in path  , I understood this approach.
source_path = Path(r'E:\data\transport_data')  # hard code path
source_path = Path(r{source_path}) 

print(source_path)
Reply
#2
All str objects are raw. Only string literals are not raw. A string command line argument is raw.
import sys
print("Literal")
print("C:\\name\things\really\badly.txt")
print("\nCommand Line")
print(sys.argv[1])
Output:
>>> python test.py "C:\\name\things\really\badly.txt" Literal ealladly.txts Command Line C:\\name\things\really\badly.txt
No escape sequence substitution because the command line argument is not a Python str literal.

Can you demonstrate why you think you need this: Path(r{source_path})
Reply


Forum Jump:

User Panel Messages

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