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))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With