2

I would like to read all the contents from all the text files in a directory. I have 4 text files in the "path" directory, and my codes are;

for filename in os.listdir(path):
    filepath = os.path.join(path, filename)
    with open(filepath, mode='r') as f:
        content = f.read()
        thelist = content.splitlines()
        f.close()
        print(filepath)
        print(content)
        print()

When I run the codes, I can only read the contents from only one text file.

I will be thankful that there are any advice or suggestions from you or that you know any other informative inquiries for this question in stackoverflow.

vdu16
  • 123
  • 10

3 Answers3

2

Basically, if you want to read all the files, you need to save them somehow. In your example, you are overriding thelist with content.splitlines() which deletes everything already in it. Instead you should define thelist outside of the loop and use thelist.append(content.splitlines) each time, which adds the content to the list each iteration

Then you can iterate over thelist later and get the data out.

Hippolippo
  • 803
  • 1
  • 5
  • 28
Amadeusz Ozga
  • 66
  • 1
  • 2
2

This should list your file and you can read them one by one. All the lines of the files are stored in all_lines list. If you wish to store the content too, you can keep append it too

from pathlib import Path
from os import listdir
from os.path import isfile, join

path = "path_to_dir"
only_files = [f for f in listdir(path) if isfile(join(path, f))]
all_lines = []
for file_name in only_files:
    file_path = Path(path) / file_name
    with open(file_path, 'r') as f:
        file_content = f.read()
        all_lines.append(file_content.splitlines())
        print(file_content)

# use all_lines

Note: when using with you do not need to call close() explicitly

Reference: How do I list all files of a directory?

shoaib30
  • 877
  • 11
  • 24
2

If you need to filter the files' name per suffix, i.e. file extension, you can either use the string method endswith or the glob module of the standard library https://docs.python.org/3/library/glob.html Here an example of code which save each file content as a string in a list.

import os

path = '.' # or your path

files_content = []

for filename in filter(lambda p: p.endswith("txt"), os.listdir(path)):
    filepath = os.path.join(path, filename)
    with open(filepath, mode='r') as f:
        files_content += [f.read()]

With the glob way here an example

import glob

for filename in glob.glob('*txt'):
    print(filename)
cards
  • 3,936
  • 1
  • 7
  • 25