0

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}")
alexdobrenko
  • 11
  • 1
  • 3
  • Python `readline` and `readlines` returns the lines with a `\n` at the end. I'm assuming the files don't have a `\n` at the end. You'll need to write code to modify each element in `Lines` to remove the trailing `\n`. e.g. `Lines = [i.strip() for i in Lines]` – algrebe Sep 03 '22 at 21:26
  • Why is this question tagged with `grep`? – Cyrus Sep 03 '22 at 21:26

2 Answers2

1

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}")
algrebe
  • 1,621
  • 11
  • 17
0

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.

paprika
  • 61
  • 4