Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error merge text files
#1
Hello!

I want to merge all of the txt-files I have in a specific folder on my computer. I looked it up and apparently you have to use the following code/command in Powershell:
copy *.txt newfile.txt
However, when I give this command, all I get is "newfile.txt" that only contains a copy of the very first txt-file in my folder, but nothing else. So it didn't really work when I tried to merge them.

Is there a way to fix this issue or an easy way to do it in Python? I looked it up, but so far I only found code snippets in which you have to type all of the filenames. Because it's more than 200 files with completely different names, this isn't a good solution for me.

Thank you in advance for your kind help!
Reply
#2
see this post which I just made to see how to get a list of files
https://python-forum.io/Thread-read-csv-...ns-missing
after that, all you need to do is add merge code.
Reply
#3
@Larz60+

Thank you!
Reply
#4
A big pitfall is, that the files are not in sorted order.
What happens if you have the wrong assumptions about glob: https://science.slashdot.org/story/19/10...ed-studies

from pathlib import Path


def get_sorted_file_list(root, pattern='*.txt', recursive=False):
    if recursive:
        extension = '**/' + pattern
    for file in sorted(Path(root).glob(pattern)):
        if file.is_file():
            yield file


text_files_sorted = list(get_sorted_file_list(root='.', pattern='*.txt', recursive=False))
The result is ordered lexical.

I guess it's important for your task, that the order is right.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,386 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  I'm trying to merge 2 .csv files with no joy! Sick_Stigma 3 958 Aug-03-2024, 03:20 PM
Last Post: mariadsouza362
  merge all xlsb files into csv mg24 0 809 Nov-13-2023, 08:25 AM
Last Post: mg24
  azure TTS from text files to mp3s mutantGOD 2 3,138 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  merge two csv files into output.csv using Subprocess mg24 9 3,541 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Merge all json files in folder after filtering deneme2 10 4,522 Sep-18-2022, 10:32 AM
Last Post: deneme2
  Writing into 2 text files from the same function paul18fr 4 2,657 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 3,148 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  select files such as text file RolanRoll 2 1,938 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 3,395 Jun-20-2022, 03:06 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