Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to read all files in a directory

Tags:

python

glob

I found this piece of code that reads all the lines of a specific file.

How can I edit it to make it read all the files (html, text, php .etc) in the directory "folder" one by one without me having to specify the path to each file? I want to search each file in the directory for a keyword.

 path = '/Users/folder/index.html'
 files = glob.glob(path)
 for name in files:  
     try:
         with open(name) as f:  
             sys.stdout.write(f.read())
     except IOError as exc:
         if exc.errno != errno.EISDIR:  
             raise
like image 825
user3702643 Avatar asked Nov 02 '14 03:11

user3702643


2 Answers

import os
your_path = 'some_path'
files = os.listdir(your_path)
keyword = 'your_keyword'
for file in files:
    if os.path.isfile(os.path.join(your_path, file)):
        f = open(os.path.join(your_path, file),'r')
        for x in f:
            if keyword in x:
                #do what you want
        f.close()

os.listdir('your_path') will list all content of a directory
os.path.isfile will check its file or not

like image 120
Hackaholic Avatar answered Oct 13 '22 21:10

Hackaholic


Update Python 3.4+

Read all files

from pathlib import Path

for child in Path('.').iterdir():
    if child.is_file():
        print(f"{child.name}:\n{child.read_text()}\n")

Read all files filtered by extension

from pathlib import Path

for p in Path('.').glob('*.txt'):
    print(f"{p.name}:\n{p.read_text()}\n")

Read all files in directory tree filtered by extension

from pathlib import Path

for p in Path('.').glob('**/*.txt'):
    print(f"{p.name}:\n{p.read_text()}\n")

Or equivalently, use Path.rglob(pattern):

from pathlib import Path

for p in Path('.').rglob('*.txt'):
    print(f"{p.name}:\n{p.read_text()}\n")

Path.open()

As an alternative to Path.read_text() [or Path.read_bytes() for binary files], there is also Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None), which is like the built-in Python function open().

from pathlib import Path

for p in Path('.').glob('*.txt'):
    with p.open() as f:
        print(f"{p.name}:\n{f.read()}\n")
like image 21
Christopher Peisert Avatar answered Oct 13 '22 21:10

Christopher Peisert