简体   繁体   中英

Python arguments passed with quotes

I'm trying to pass file list to my python script via argument:

python script.py -o aaa -s bbb "filename.txt" "filename2.txt" "file name3.txt"

Unfortunately ArgumentParser is ignoring quotes and instead of giving list of 3 files it gives me list of 4 elements as followed:

1) "filename.txt"
2) "filename2.txt"
3) "file
4) name3.txt"

It completely ignores quotes. How to make it work with quotes?

Hard without seeing what you're using or any code. Your shell may be interfering, you may need to escape the spaces with \\ .

Example: python script.py -oa -f "file1.txt" "file\\ 2.csv"

Is hard without code but considering you are using sys.argv[] you can easily pass the file arguments with quotes like when you need a file or argv with blank spaces: python script.py "myFile.txt" "otherFile.jpeg" Try this simple code to understand:

import sys

for n, p in enumerate(sys.argv):
    print("Parameter: %d = %s") % (n, p))`

You can see that first argv is the file name you are running.

It looks like that this is not python's fault. I'm calling python script from inside of bash script and this make mess with quotes as parameters.

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