Python Forum
Merge htm files with shutil library (TypeError: 'module' object is not callable)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge htm files with shutil library (TypeError: 'module' object is not callable)
#7
If you run the program twice, then also the content of "ouput_file.htm" is included to "output_file.htm", but "output_file.htm" gets new content, because the output is read and written from the same file. I tried this on a NVMe and file size was growing very fast. I deleted afterward 4 GiB data because of this silly mistake.

Example with Path objects
from pathlib import Path
from shutil import copyfileobj


def merge(path: str|Path, glob: str, output: str|Path, show:bool=False) -> None:
    """
    Merge files found in path by glob pattern.
    All data is written to output.

    Args:
        path (str | Path): Path to find files
        glob (str): glob pattern to find files in path
        output (str | Path): Output file
        show (bool, optional): Print processed file. Defaults to False.
    """
    # Ensure that output is a Path object
    output = Path(output)
    
    # excluding output file from the list of files
    files = [file for file in Path(path).glob(glob) if file != output]
    
    # keep in mind, that the order of files is not given
    # sorting files by modification time
    # but I guess it's not what you want
    files.sort(key=lambda file: file.stat().st_mtime)

    if not files:
        return

    with open(output, "wb") as fd_out:
        for file in files:
            if show:
                print(f"Processing {file}")

            with file.open("rb") as fd_in:
                copyfileobj(fd_in, fd_out)


merge(".", "*.txt", "output_file.txt", show=True)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Merge htm files with shutil library (TypeError: 'module' object is not callable) - by DeaD_EyE - Mar-08-2025, 01:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to merge 2 .csv files with no joy! Sick_Stigma 3 955 Aug-03-2024, 03:20 PM
Last Post: mariadsouza362
  I am getting this TypeError: 'TreasureMap' object is not subscriptable. makilakos 2 1,230 May-25-2024, 07:58 PM
Last Post: deanhystad
  Using zipfile module - finding folders not files darter1010 2 2,091 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 2,705 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 1,738 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  use of shutil.copytree with ENOTDIR exception yan 2 2,711 Nov-29-2023, 03:02 PM
Last Post: yan
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 3,583 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  merge all xlsb files into csv mg24 0 808 Nov-13-2023, 08:25 AM
Last Post: mg24
  TypeError: 'NoneType' object is not callable akbarza 4 13,945 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 5,593 Aug-23-2023, 06:21 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020