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

MRAB python at mrabarnett.plus.com
Mon Apr 18 21:17:15 EDT 2016


On 2016-04-19 00:44, Sayth Renshaw wrote:
> Hi
>
> Why would it be that my files are not being found in this script?
>
> from pyquery import PyQuery as pq
> import pandas as pd
> import os
> import sys
>
> if len(sys.argv) == 2:
>      print("no params")
>      sys.exit(1)
>
> dir = sys.argv[1]
> mask = sys.argv[2]
>
> files = os.listdir(dir)
>
> fileResult = filter(lambda x: x.endswith(mask), files)
>
> # d = pq(filename='20160319RHIL0_edit.xml')
> data = []
>
> for file in fileResult:
>      print(file)
>
> for items in fileResult:
>      d = pq(filename=items)
>      res = d('nomination')
>      attrs = ('id', 'horse')
>      data = [[res.eq(i).attr(x) for x in attrs] for i in range(len(res))]
>
>      # from nominations
> # res = d('nomination')
> # nomID = [res.eq(i).attr('id') for i in range(len(res))]
> # horseName = [res.eq(i).attr('horse') for i in range(len(res))]
>
> # attrs = ('id', 'horse')
>
> frames = pd.DataFrame(data)
> print(frames)
>
>
> I am running this from the bash prompt as
>
> (pyquery)sayth at sayth-E6410:~/Projects/pyquery$ python jqxml.py samples *.xml
>
> my directory structure
>
> (pyquery)sayth at sayth-E6410:~/Projects/pyquery$ ls -a
> .  ..  environment.yml  .git  .gitignore  #jqxml.py#  jqxml.py  samples
>
> and samples contains
>
> (pyquery)sayth at sayth-E6410:~/Projects/pyquery/samples$ ls -a
> .   20160319RHIL0_edit.xml  20160409RAND0.xml
> ..  20160402RAND0.xml       20160416RAND0.xml
>
> yet I get no files out of the print statement.
>
> Ideas?
>
I don't use Linux, but I think it might be a problem with what you have 
on the command line. I believe that Linux expands wildcarded names, so 
what you might be getting is "samples" followed by all the names in the 
current directory that match "*.xml".

Even if that isn't the case, and mask is "*.xml", the filtering that 
you're doing is asking for those names that end with "*.xml"; you might 
find a name that ends with ".xml", but I doubt you'll ever find one that 
ends with "*.xml"!




More information about the Python-list mailing list