'Python: How to read all files in a directory?

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


Solution 1:[1]

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

Solution 2:[2]

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")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2