Home > Software design >  Is there a way to pass in a str argument containing "$" character? [duplicate]
Is there a way to pass in a str argument containing "$" character? [duplicate]

Time:09-22

My python script requires an argument of type str.

I am using the ArgumentParser class from argparse.

I want to pass in a string containing a "$" char in the middle, e.g. "file$102_a.txt", but when passed to the script the argument is stored as "file" cutting off the $102_a.

Is there any way to enable argparse to do accomodate this?

Running macOS, python v3.7.6

Thank you for your help.

CodePudding user response:

We can pass it with an escape sequence before the dollar sign.

Say test.py is defined as

import sys
print(sys.argv[1])

Then,

$ python test.py "dff\$sdfs"

Would print dff$sdfs

EDIT: As pointed out by user: smac89, if the arguments are enclosed within single quotes ('), instead of double quotes ("), it will not split at the dollar sign.

$ python test.py 'dff$sdfs'

Would also print dff$sdfs

  • Related