Home > Blockchain >  finding matches between a) all the files in a directory and b) a txt list of files not working with
finding matches between a) all the files in a directory and b) a txt list of files not working with

Time:09-04

So I've got the code below and when I run tests to spit out all the files in A1_dir and A2_list, all of the files are showing up, but when I try to get the fnmatch to work, I get no results.

For background in case its helpful: I am trying to comb through a directory of files and take an action (duplicate the file) only IF it matches a file name on the newoutput.txt list. I'm sure there's a better way to do all of this lol, so if you have that I'd love to hear it too!

import fnmatch
import os 

A1_dir = ('C:/users/alexd/kobe')

A2_list = open('C:/users/alexd/kobe/newoutput.txt')
Lines = A2_list.readlines()
A2_list.close()

for file in (os.listdir(A1_dir)):
    for line in Lines: 
        if fnmatch.fnmatch(file, line):
            print("got one:{file}")

CodePudding user response:

You cannot use fnmatch.fnmatch to compare 2 different filenames, fnmatch.fnmatch only accepts 2 parameters filename and pattern respectively.

As you can see in the official documentation:

official documentation

Possible Solution:

I don't think that you have to use any function to compare 2 strings. Both os.listdir() and .readlines() returns you lists of strings.

CodePudding user response:

readline returns a single line and readlines returns all the lines as a list (doc). However, in both cases, the lines always have a trailing \n i.e. the newline character.

A simple fix here would be to change

Lines = A2_list.readlines()

to

Lines = [i.strip() for i in A2_list.readlines()]

Since you asked for a better way, you could take a look at set operations. Since the lines are exactly what you want the file names to be (and not patterns), save A2_list as a set instead of a list.

Next, save all the files from os.listdir also as a set.

Finally, perform a set intersection

import fnmatch
import os

with open('C:/users/alexd/kobe/newoutput.txt') as fp:
    myfiles = set(i.strip() for i in fp.readlines())

all_files = set(os.listdir('C:/users/alexd/kobe'))

for f in all_files.intersection(myfiles):
        print(f"got one:{f}")
  • Related