简体   繁体   中英

python fnmatch unable to find the file

I have a directory that has bunch of sub directories, each subdir has many csv files, but I am only interest in certain csv file. So I wrote following python method, but I am unable to capture the file name, if I do *.csv it will find all the file but I don't want to all the files to be read in:

def gatherStats(template_file, csv_file):
    for lang in getLanguageCodes(csv_file):
        lang_dir = os.path.join(template_file, lang)
        try:
            for file in os.listdir(lang_dir):
                if fnmatch.fnmatch(file, '*-*-template-users-data.csv'):
                    t_file = open(file, 'rb').read()
                    reader = csv.reader()
                    for row in reader:
                        print row
                else:
                    print "didn't find the file"
        except Exception, e:
            logging.exception(e)

What am I doing wrong here? Is it a regular expression issue? Can we use regular expression with fnmath?

There are several problems with your code. Fix them first, then we might get to the bottom of what your issue really is.

  • First of all, don't use built-in names as variables, such as file . Rather replace it with filename .
  • Then os.path.join(lang_dir, filename) before opening the file. Meaning:

     t_file = open(os.path.join(lang_dir, filename), 'rb').read() 
  • How do you expect reader = csv.reader() to read your file if you don't reference your open file object in this line?

  • Your try / except block is a bit too wide for my taste. Take your time and narrow down the errors that actually can happen. Then decide which of them you want to ignore and which should crash your program. Take a close look at the exceptions actually thrown in this block. You'll probably find your issue there.

With the help provided by another user, I manage to fix the problem. I am putting this answer here for future reference for community.

def gatherStats(template_file, csv_file):
    for lang in getLanguageCodes(csv_file):
        lang_dir = os.path.join(template_file, lang)
        try:
            for filename in os.listdir(lang_dir):
                path = os.path.join(lang_dir, filename)
                if re.search(r'-.+-template-users-data.csv$',filename):
                    with open(path, 'rb') as template_user_data_file:
                        reader = csv.reader(template_user_data_file)
                        try:
                            for row in reader:
                                print row
                        except csv.ERROR as e:
                            logging.error(e)
                else:
                    print "didn't find the file"
        except Exception, e:
            logging.exception(e)

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