For python 2.7:
pip install glob2
For python 3.7:
pip3 install glob2
I'm trying to code a little program that will open a CSV file that we should (theoretically) download weekly. The csv filename has the structure "report[numbers].csv" I'd read that you need the glob module so that Python can recognize the wildcard, but I am getting an error when I go the usual python pip -m SomeName route.
Is there an issue with glob, or is there another I can use?
I tried with different Scenarios,and finally got exact solution which satisfies all my conditions. First I'm checking for user input file is available or not in the specified directory, If it is available then globing all files with same file with (_) all at the end appending match file to same list.
If user input if not file is not available in the specified directory then I'm checking for the files with (_) symbol then globing all files into list. At the end iterating through list and got final result.
Program-
from glob import glob
import os
import sys
file_pattern = ''
files_list = list()
arguments = {'ABC', 'PQR', 'XYZ'}
#checking for user provided argument or not
if len(sys.argv[1:2]) is 1:
file_pattern = str(sys.argv[1:2])
else:
print 'run as < python test.py <LineName> >'
sys.exit(1)
#replace all unnecessary stuff with ('')
file_pattern = file_pattern.replace('[','').replace(']','').replace('\'','')
#checking for line number is provided or not
if file_pattern in arguments:
print '<Provide LineName with some Number>'
sys.exit(1)
flag = True
#list of all files containing specified directory
files = os.listdir('<directory name>')
for file_name in files:
if str(file_name) == str(file_pattern)+'.csv':
files_list = glob(os.path.join('<directory name>', str(file_pattern)+'_*.csv'))
#appending match file also to resultant list
files_list.append('<directory name>'+file_name)
flag = False
#if specified file is not present in dir check for filename with (_)
if flag:
files_list = glob(os.path.join('<directory name>', str(file_pattern)+'_*.csv'))
#checking for list contains items or not
if files_list:
for a_file in sorted(files_list):
print a_file
else:
print 'No Such File --> ' + str(file_pattern)+ '\t <Provide appropriate Name1>'
sys.exit(1)
Consider directory contains ABC1.csv, ABC1_1.csv, ABC1_2.csv, ABC11.csv, ABC11_1.csv, ABC11_3.csv, ABC11_2.csv files.
Output Scenario :
#if input is ABC1
.\\ABC1.csv
.\\ABC1_1.csv
.\\ABC1_2.csv
#if input is ABC11
.\\ABC11.csv
.\\ABC11_1.csv
.\\ABC11_2.csv
.\\ABC11_3.csv
I have a solution. It's not perfect, depends if you have other files in the folder:
file_pattern = 'ABC1'
files_list = glob(os.path.join('<directory name>', str(file_pattern)+'[!0-9]*'))
# output: ABC1.csv, ABC1_1.csv, ABC1_2.csv
file_pattern = 'ABC11'
files_list = glob(os.path.join('<directory name>', str(file_pattern)+'[!0-9]*'))
# output: ['.\\ABC11.csv', '.\\ABC11_1.csv', '.\\ABC11_2.csv', '.\\ABC11_3.csv']
I had the same problem as Jesper. The issue is that although * will match any character, it needs a character!
By selecting any file that doesn't have a digit after the file pattern, we avoid the 1-11 issue.