AOJ keygen
AOJ keygen

Reputation: 103

Use pandas.read_csv to read files containing str() in file's name

I am trying to use pandas.read_csv to read files that contain the date in their names. I used the below code to do the job. The problem is that the files name is not consistent as the number of date change the pattern. I was wondering if there is a way to let the code read the file with parts of the name is the date in front of the file name?

enter image description here

for x in range(0,10):
dat = 20170401+x
dat2 = dat+15
file_name='JS_ALL_V.'+str(dat)+'_'+str(dat2)+'.csvp.gzip'
df = pd.read_csv(file_name,compression='gzip',delimiter='|')

Upvotes: 0

Views: 1760

Answers (2)

Vipin
Vipin

Reputation: 5223

You can use glob library to read file names in unix style

Below is its hello world:

import glob
for name in glob.glob('dir/*'):
    print name

Upvotes: 1

toom501
toom501

Reputation: 354

An alternative of using glob.glob() (since it seems not working) is os.listdir() as explained in this question in order to have a list containing all the elements (or just the files) in your path.

Upvotes: 1

Related Questions