I want read files in one directory.
Directory contains :
ABC1.csv
ABC1_1.csv
ABC1_2.csv
ABC11.csv
ABC11_1.csv
ABC11_3.csv
ABC11_2.csv
ABC13_4.csv
ABC13_1.csv
ABC17_6.csv
ABC17_2.csv
ABC17_4.csv
ABC17_8.csv
While running script I want to give command line argument for reading specific files depend on some conditions :
- If user give only ABC error message.
- If user give ABC1 then it must read ABC1.csv, ABC1_1.csv and ABC1_2.csv only.
- If user give ABC11 then it must read ABC11.csv,ABC11_1.csv,ABC11_2.csv,ABC11_3.csv only.
- If user give ABC13 it must read ABC13_1.csv,ABC13_4.csv only.
- If user give ABC17 then it must read ABC17_2.csv,ABC17_4.csv,ABC17_6.csv,ABC17_8.csv only.
For this stuff I'm created a script but I'm facing issue.
Program-
from glob import glob
import os
import sys
file_pattern = ''
files_list = list()
arguments = {'ABC', 'PQR', 'XYZ'}
if len(sys.argv[1:2]) is 1:
file_pattern = str(sys.argv[1:2])
else:
print 'run as <python test.py ABC>'
sys.exit(1)
if file_pattern in arguments:
print '<Provide Name with some Number>'
sys.exit(1)
file_pattern = file_pattern.replace('[','').replace(']','').replace('\'','')
if file_pattern.startswith('ABC',0,3):
files_list = glob(os.path.join('<directory name>', str(file_pattern)+'_*.csv'))
else:
print 'No Such File --> ' + str(file_pattern)+ '\t <Provide appropriate Name>'
sys.exit(1)
if files_list:
for a_file in sorted(files_list):
print a_file
#process file
else:
print 'No Such File --> ' + str(file_pattern)+ '\t <Provide appropriate Name>'
sys.exit(1)
This code is working fine but it doesn't satisfy my 2nd condition. when user is giving ABC1 as a argument i.e. python test.py ABC1 , it will return files ABC1_1.csv, ABC1_2.csv but not returning ABC1.csv file.
How I can satisfy this 2nd condition also without losing any other condition?