问题
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?
回答1:
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 -o a -f "file1.txt" "file\ 2.csv"
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/17297656/python-arguments-passed-with-quotes