Reputation: 2567
Im trying to provide wildcard support to one of CLI
scripts and I am using pythons glob
module for it. For a test i tried this:
>>> import glob
>>> for f in glob.glob('/Users/odin/Desktop/test_folder/*.log'):
... print f
...
/Users/odin/Desktop/test_folder/test1.log
/Users/odin/Desktop/test_folder/test2.log
/Users/odin/Desktop/test_folder/test3.log
This works perfectly and gives the right output as i do have the 3 files given above.
However when I have the same code under my argument in the CLI
it fails. I do this
#code...
parser.add_argument( "-f", "--file", type=str, help="Full path to the file to upload." )
#code...
if args.file:
for f in glob.glob(args.file):
_upload_part(f)
I run this as
python cli.py -f /Users/odin/Desktop/test_folder/*.log
This gives me the error:
cli.py: error: unrecognized arguments: /Users/odin/Desktop/test_folder/test2.log /Users/odin/Desktop/test_folder/test3.log
I dont understand why all files are being added to the argument at once when I am going through the list one by one.
EDIT-
nargs
was a step in the right direction but now this error shows up:
`Traceback (most recent call last):
File "cli.py", line 492, in <module>
for f in glob.glob(args.file):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/glob.py", line 27, in glob
return list(iglob(pathname))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/glob.py", line 38, in iglob
dirname, basename = os.path.split(pathname)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 92, in split
i = p.rfind('/') + 1
AttributeError: 'list' object has no attribute 'rfind'`
Upvotes: 1
Views: 2662
Reputation: 46513
Here's what your script receives:
python cli.py -f file1 file2 file3 fileN
Where N is a total number of files matched by that pattern (Shell expands wildcards automatically). On the other hand, your file argument is configured to receive just one file/pattern, thus the simplest (and the best, in my opinion) solution is to add the nargs='+'
argument:
parser.add_argument('-f', '--file', type=str, help='...', nargs='+')
# code
for f in args.file:
print(f)
This will allow you to remove all glob
calls and if args.file
checks.
Another option is to quote your command line argument:
python cli.py -f '/Users/odin/Desktop/test_folder/*.log'
Upvotes: 3