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

how to apply r(raw string) for a variable path
r"\\IND200300400.XXX.XXXX.XXX\Recon Project\output1" ---------here directly attached r its ok.

folderpath = \\IND200300400.XXX.XXXX.XXX\
path = r(folderpath)?...... how to apply r for variable. if user pass variable.


def createfolder(folderpath,site):

    Constant_folder = r{folderpath}
    Constant_folder = r(folderpath)


    path = f"{Constant_folder}\{site}"
    try:
        os.makedirs(path, exist_ok=True)
        print("Directory '%s' created successfully" % path)
    except OSError as error:
        print("Directory '%s' can not be created")



if __name__ == "__main__":
    folderpath =  "\\IND200300400.XXX.XXXX.XXX\Recon Project\output1"
    site = "US"
    createfolder(folderpath, site)
Reply
#2
raw is only a thing for string literals. Normally when Python encounters a string while "compiling" your program, it replaces escape sequences with the actual un-printable character. The "r" prefix before a string literal tells Python to not look for escape sequences when making the str object. After the program compile stage, "raw" strings are not a thing anymore. All str objects are essentially "raw" strings, because none of them contain escape sequences.
if __name__ == "__main__":
    folderpath =  r"\\IND200300400.XXX.XXXX.XXX\Recon Project\output1"    # <- The "r" belongs here
    site = "US"
    createfolder(folderpath, site)
Reply
#3
Hi deanhystad,

I am accepting folderpath from commandline.
python test1.py "\\IND200300400.XXX.XXXX.XXX\Recon Project\output1"

folderpath = sys.argv[1]

folderpath = r{folderpath } something like this.

thanks
mg
Reply
#4
If it is coming from the command line than it is already a str object and not a str literal. raw does not apply.

I did a little experiment. I wrote a program to spit out the command line argument
import sys
print(f"{sys.argv[1]}, {repr(sys.argv[1])}, {len(sys.argv[1])}"
I ran the program as "python text.py "\\\n". This was the output.
Output:
\\\n, '\\\\\\n', 4
The repr version of the command line argument looks odd, but the length shows this is an artifact of repr trying to show you that the string really contains "\\" and "\n" and not escape<\> and escape<n>.

If backslashes are giving you headaches, use forward slashes. Windows accepts forward slash as a path seperator.
ibreeden likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 795 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Need help on how to include single quotes on data of variable string hani_hms 5 2,059 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  USE string data as a variable NAME rokorps 1 971 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,313 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,811 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,311 Jul-11-2022, 08:52 AM
Last Post: japo85
  Can you print a string variable to printer hammer 2 1,967 Apr-30-2022, 11:48 PM
Last Post: hammer
Question How to convert string to variable? chatguy 5 2,442 Apr-12-2022, 08:31 PM
Last Post: buran
  I want to search a variable for a string D90 lostbit 3 2,636 Mar-31-2021, 07:14 PM
Last Post: lostbit
  Add variable value to string pythonlearner1 5 2,989 May-25-2020, 05:51 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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