Opening And Reading All The Files In A Directory In Python - Python Beginner
I'd like to read the contents of every file in a folder/directory and then print them at the end (I eventually want to pick out bits and pieces from the individual files and put th
Solution 1:
- Separate the different functions of the thing you want to do.
- Use generators wherever possible. Especially if there are a lot of files or large files
Imports
from pathlib importPathimport sys
Deciding which files to process:
source_dir = Path('results/')
files = source_dir.iterdir()
[Optional] Filter files
For example, if you only need files with extension .ext
files = source_dir.glob('*.ext')
Process files
defprocess_files(files):
for file in files:
with file.open('r') as file_handle :
for line in file_handle:
# do your thingyield line
Save the lines you want to keep
def save_lines(lines, output_file=sys.std_out):
for line inlines:
output_file.write(line)
Solution 2:
you forgot indentation at this line allLines = file.readlines()
and maybe you can try that :
import os
allLines = []
path = 'results/'
fileList = os.listdir(path)
for file in fileList:
file = open(os.path.join('results/'+ i), 'r')
allLines.append(file.read())
print(allLines)
Solution 3:
You forgot to indent this line allLines.append(file.read())
.
Because it was outside the loop, it only appended the file
variable to the list after the for
loop was finished. So it only appended the last value of the file
variable that remained after the loop. Also, you should not use readlines()
in this way. Just use read()
instead;
import os
allLines = []
path = 'results/'
fileList = os.listdir(path)
for file in fileList:
file = open(os.path.join('results/'+ i), 'r')
allLines.append(file.read())
print(allLines)
Solution 4:
This also creates a file containing all the files you wanted to print.
rootdir= your folder, like 'C:\\Users\\you\\folder\\'
import os
f = open('final_file.txt', 'a')
for root, dirs, files inos.walk(rootdir):
for filename in files:
data = open(full_name).read()
f.write(data + "\n")
f.close()
This is a similar case, with more features: Copying selected lines from files in different directories to another file
Post a Comment for "Opening And Reading All The Files In A Directory In Python - Python Beginner"