Skip to content Skip to sidebar Skip to footer

Open Every File/subfolder In Directory And Print Results To .txt File

At the moment I am working with this code: from bs4 import BeautifulSoup import glob import os import re import contextlib @contextlib.contextmanager def stdout2file(fname):

Solution 1:

For walking in subdirectories, there are two options:

  1. Use ** with glob and the argument recursive=True (glob.glob('**/*.html')). This only works in Python 3.5+. I would also recommend using glob.iglob instead of glob.glob if the directory tree is large.

  2. Use os.walk and check the filenames (whether they end in ".html") manually or with fnmatch.filter.


Regarding the printing into a file, there are again several ways:

  1. Just execute the script and redirect stdout, i.e. python3 myscript.py >myfile.txt

  2. Replace calls to print with a call to the .write() method of a file object in write mode`.

  3. Keep using print, but give it the argument file=myfile where myfile is again a writable file object.

edit: Maybe the most unobstrusive method would be the following. First, include this somewhere:

import contextlib
@contextlib.contextmanagerdefstdout2file(fname):
    import sys
    f = open(fname, 'w')
    sys.stdout = f
    yield
    sys.stdout = sys.__stdout__
    f.close()

And then, infront of the line in which you loop over the files, add this line (and appropriately indent):

withstdout2file("output.txt"):

Post a Comment for "Open Every File/subfolder In Directory And Print Results To .txt File"