Please help me, i have some file txt in folder. I want to read and summary all data become one file txt. How can I do it with python.
for example :
folder name : data
file name in that folder : log1.txt
data in log1.txt : Size: 1,116,116,306 bytes
data in log2.txt : Size: 1,116,116,806 bytes
data in log3.txt : Size: 1,457,116,806 bytes
data in log4.txt : Size: 1,457,345,000 bytes
a file txt the result.txt and the data is : 1,116,116,306
Did you mean you want to read the contents of each file and write all of them in to a different file.
import os #returns the names of the files in the directory data as a list list_of_files = os.listdir("data") lines= for file in list_of_files: f = open(file, "r") #append each line in the file to a list lines.append(f.readlines()) f.close() #write the files to result.txt result = open("result.txt", "w") result.writelines(lines) result.close()
If you are looking for size of file instead of the contents. change the two lines :
f= open(file,"r") lines.append(f.readlines())