Python Forum

Full Version: python raw string help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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})