0

I want to read csv files from a folder based on condition. I just want to read csv files that include “1441” in the filename. I used fnmatch, but it doesn’t work. Can anyone help?

path_to_parent = r"C:\Users\Desktop\books/chapter_1"

for csv_file in os.listdir(path_to_parent):
    if fnmatch.fnmatch(csv_file,'1441'):
        my_file = pd.read_csv(path_to_parent+csv_file)
    else:
        print('error')
0

2 Answers 2

2

You need wildcards around 1441 to match the rest of the filename. Otherwise it's looking for the exact filename 1441.

Also, you're not adding the directory separator between path_to_parent and csv_file when you concatenate them. It's best to use os.path.join() for portability.

for csv_file in os.listdir(path_to_parent):
    if fnmatch.fnmatch(csv_file,'*1441*'):
        my_file = pd.read_csv(os.path.join(path_to_parent, csv_file))
    else:
        print('error')

I also recommend using glob.glob() instead. It will do the wildcard matching for you, and it will return full paths so you don't have to concatenate each time through the loop.

for csv_file in glob.glob(os.path.join(path_to_parent, '*1441*')):
    my_file = pd.read_csv(csv_file)
Sign up to request clarification or add additional context in comments.

Comments

0

You could try different approach with slight modification to your if statement.

for csv_file in os.listdir(path_to_parent):
        if '1441' in csv_file:
            my_file = pd.read_csv(f'{path_to_parent}/{csv_file}')
        else:
            print('error')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.