Python Forum
Sorting and Merging text-files [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting and Merging text-files [SOLVED]
#1
Hello everybody,

With a batch-script I export mutliple text-files to a specific folder where I want to merge them into one.
Therefore I used the type-command in windows cmd which worked fine. But after comparing the files I noticed that the order is wrong. I found out the CMD sorts them differently (file_1,file_10,file_11,file_2,file_3,file_33) instead of how I'm doing it (file_1,file_2,file_3,file_10,file_11,file_33).

Now I would like to use a Python Script do merge them together in the way I would sort them (file_1,file_2,file_3,file_10,file_11,file_33).

This is what I have so far but I don't know how to go on:

#!/usr/bin/env python3

import os
import re

folder_path = "../Outputs/"

for root, dirs, files in os.walk(folder_path, topdown = False):
    for name in files:
        if name.endswith(".txt"):
            file_name = os.path.join(root, name)
Edit: All my files start with string "file_" and an ongoing number. It differs how many files there will be after I exported them so I can't set it up manually.
Reply


Messages In This Thread
Sorting and Merging text-files [SOLVED] - by AlphaInc - Aug-19-2021, 01:03 PM
RE: Sorting and Merging text-files - by Gribouillis - Aug-19-2021, 02:08 PM
RE: Sorting and Merging text-files - by DeaD_EyE - Aug-19-2021, 02:08 PM
RE: Sorting and Merging text-files - by AlphaInc - Aug-19-2021, 05:02 PM
RE: Sorting and Merging text-files - by deanhystad - Aug-19-2021, 04:16 PM
RE: Sorting and Merging text-files - by snippsat - Aug-19-2021, 10:12 PM
RE: Sorting and Merging text-files - by AlphaInc - Aug-20-2021, 05:48 AM
RE: Sorting and Merging text-files - by AlphaInc - Aug-20-2021, 08:12 AM
RE: Sorting and Merging text-files - by AlphaInc - Aug-20-2021, 08:13 AM
RE: Sorting and Merging text-files - by snippsat - Aug-20-2021, 10:14 AM
RE: Sorting and Merging text-files - by snippsat - Aug-20-2021, 05:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 273 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
Question [solved] compressing files with python. SpongeB0B 1 682 May-26-2023, 03:33 PM
Last Post: SpongeB0B
  Help replacing word in Mutiple files. (SOLVED) mm309d 0 874 Mar-21-2023, 03:43 AM
Last Post: mm309d
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,226 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  azure TTS from text files to mp3s mutantGOD 2 1,752 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  [SOLVED] [BeautifulSoup] How to get this text? Winfried 6 2,033 Aug-17-2022, 03:58 PM
Last Post: Winfried
  Writing into 2 text files from the same function paul18fr 4 1,726 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,631 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Human Sorting (natsort) does not work [SOLVED] AlphaInc 2 1,182 Jul-04-2022, 10:21 AM
Last Post: AlphaInc
  select files such as text file RolanRoll 2 1,205 Jun-25-2022, 08:07 PM
Last Post: RolanRoll

Forum Jump:

User Panel Messages

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