Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opening and reading all the files in a directory in python - python beginner

Tags:

python

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 them in a separate document) So far I have this code

import os
path = 'results/'
fileList = os.listdir(path)
for i in fileList:
    file = open(os.path.join('results/'+ i), 'r')
allLines = file.readlines()
print(allLines)

at the end I dont get any errors but it only prints the contents of the last file in my folder in a series of strings and I want to make sure its reading every file so I can then access the data I want from each file. I've looked online and I cant find where I'm going wrong. Is there any way of making sure the loop is iterating over all my files and reading all of them?

also i get the same result when I use

 file = open(os.path.join('results/',i), 'r')

in the 5th line

Please help I'm so lost Thanks!!

like image 627
Hannah Baker Avatar asked Feb 12 '26 08:02

Hannah Baker


2 Answers

  • 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 import Path
import 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

def process_files(files):
    for file in files:
        with file.open('r') as file_handle :
            for line in file_handle:
                # do your thing
                yield line

Save the lines you want to keep

def save_lines(lines, output_file=sys.std_out):
    for line in lines:
        output_file.write(line)
like image 161
Maarten Fabré Avatar answered Feb 15 '26 00:02

Maarten Fabré


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)
like image 37
Rayane Bouslimi Avatar answered Feb 14 '26 23:02

Rayane Bouslimi



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!