read all the files in a directory in python -


i pretty new python.i have directory , inside have 2 sub directory.each sub directory contain 100 text file.i want read content of each file of both sub directory , put them in single text file in such way each file content in single line in new file.how can achieve in pyhon.thank you

since don't have anything.. try starting here. can use glob module instead load files single level of subdirectories, or use os.walk() walk arbitrary directory structure depending on requirement,

to open text files in arbitrary nesting of directories:

 import os  import fnmatch   dirpath, dirs, files in os.walk('test'):     filename in fnmatch.filter(files, '*.txt'):         open(os.path.join(dirpath, filename)):             # deal file next loop present new file.              #use filename object whatever want file 

since new said. watch out indentations. #goodluck


Comments