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 all xlsb files into csv mg24 0 332 Nov-13-2023, 08:25 AM
Last Post: mg24
  azure TTS from text files to mp3s mutantGOD 2 1,695 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  merge two csv files into output.csv using Subprocess mg24 9 1,756 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Merge all json files in folder after filtering deneme2 10 2,336 Sep-18-2022, 10:32 AM
Last Post: deneme2
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,584 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  Writing into 2 text files from the same function paul18fr 4 1,671 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,546 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  select files such as text file RolanRoll 2 1,163 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 1,908 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,509 Apr-04-2022, 09:29 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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