Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude some files when reading with glob.glob()?

I am reading some files using glob.glob(). I want to read all the files with names 123*.txt except those with 123*error.txt. Also, is there a way to print the filenames in the for loop, which is inside pd.concat()?

fields = ['StudentID', 'Grade']
path= 'C:/script_testing/'

parse = lambda f: pd.read_csv(f, usecols=fields)
table3 = pd.concat(
[parse(f) for f in glob.glob('C:/script_testing/**/*.txt', recursive=True)]
).pipe(lambda d: pd.crosstab(d.StudentID, d.Grade))
like image 290
Sikander Waheed Avatar asked Oct 16 '25 15:10

Sikander Waheed


1 Answers

Use this pattern

files = glob.glob('C:/script_testing/**/123*[!error].txt', recursive=True)

Then proceed

fields = ['StudentID', 'Grade']
path= 'C:/script_testing/'

parse = lambda f: pd.read_csv(f, usecols=fields)
table3 = pd.concat(
    [parse(f) for f in files]
).pipe(lambda d: pd.crosstab(d.StudentID, d.Grade))

Reference this post

like image 143
piRSquared Avatar answered Oct 18 '25 06:10

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!