I am using the current function to read file from a local directory when the filename starts with certain prefix. Now I have second use case where multiple prefixes are in a list like['prefix1', 'prefix2'] . How can I modify the code below for finding multiple filenames from the local directory where filename start with prefixes in the list? If I use the current function than it gives error as now the prefix is not string but list.
if dev:
print('Reading files from dowloaded data')
for filename in os.listdir('Data\Input_data'):
if prefix in filename:
print ('using downloaded file',filename)
df_file = os.path.join ('Data\Input_data',filename)
if df == None:
df = df_file
else:
df.append(df_file)
CodePudding user response:
The issue is about this line:
if prefix in filename
The logic will be inversed like this:
if (some part of the filename) in prefix
, where prefix
is a list like['prefix1', 'prefix2',..]
You can solve this using startswith
method and a tuple
of the prefixes list.
For example:
prefix_list=['prefix1', 'prefix2','prefix3', 'prefix3']
filename="prefix30000"
print(filename.startswith(tuple(prefix_list)))
#True
Then you can modify your code like this:
if dev:
print('Reading files from dowloaded data')
for filename in os.listdir('Data\Input_data'):
if filename.startswith(tuple(prefix)): # << Updated line
print ('using downloaded file',filename)
df_file = os.path.join ('Data\Input_data',filename)
if df == None:
df = df_file
else:
df.append(df_file)