Why are my files in in my list - os module used with sys argv

Steven D'Aprano steve at pearwood.info
Mon Apr 18 20:19:20 EDT 2016


On Tue, 19 Apr 2016 09:44 am, Sayth Renshaw wrote:

> Hi
> 
> Why would it be that my files are not being found in this script?

You are calling the script with:

python jqxml.py samples *.xml

This does not do what you think it does: under Linux shells, the glob *.xml
will be expanded by the shell. Fortunately, in your case, you have no files
in the current directory matching the glob *.xml, so it is not expanded and
the arguments your script receives are:


"python jqxml.py"  # not used

"samples"  # dir

"*.xml"  # mask


You then call:

fileResult = filter(lambda x: x.endswith(mask), files)

which looks for file names which end with a literal string (asterisk, dot,
x, m, l) in that order. You have no files that match that string.

At the shell prompt, enter this:

touch samples/junk\*.xml

and run the script again, and you should see that it now matches one file.

Instead, what you should do is:


(1) Use the glob module:

https://docs.python.org/2/library/glob.html
https://docs.python.org/3/library/glob.html

https://pymotw.com/2/glob/
https://pymotw.com/3/glob/


(2) When calling the script, avoid the shell expanding wildcards by escaping
them or quoting them:

python jqxml.py samples "*.xml"



-- 
Steven




More information about the Python-list mailing list