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')
CodePudding user response:
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)
CodePudding user response:
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')