Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read csv files from a folder based on condition in Python

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?

Thanks in advance

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')

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading