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
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 directoryos.path.isfile
will check its file or not
from pathlib import Path
for child in Path('.').iterdir():
if child.is_file():
print(f"{child.name}:\n{child.read_text()}\n")
from pathlib import Path
for p in Path('.').glob('*.txt'):
print(f"{p.name}:\n{p.read_text()}\n")
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")
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With