简体   繁体   中英

Passing arguments to a python program

I have a python program that receives a file path as an argument.

The problem is, that if the file path has the following chars: "%cd", then it replaces "%cd" with the current directory.

So for example:

python program.py  "C:\%af%32%cd%7f.htm"

The sys.argv (in program.py) shows this:

['program.py', 'C:\\%af%32C:\\%7f.htm']

Why is that happening and how can it be solved?

Edit

The problem is that if I put "%cd" in the command line, then in python I get the command line with the current directory string instead the "%cd" chars

Escaping % with %% only works within batch files, not on the commandline. It is possible to escape % with double quote

eg: python untitled0.py "C:\\\\"%"af"%"32"%"cd"%"7f.htm"

['untitled0.py', 'C:\\"%af%32%cd%7f.htm']

But this is a bit tedious - and you have to be careful of \\ escaping any of the " .

Another alternative would be to replace % with some other symbol and then change it back inside the python program?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM