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]
#3
#!/usr/bin/env python3

from pathlib import Path


def sort_by_int(path):
    # Path has the stem attribute, which is
    # the filename without the last extension
    # to sort the paths by integer, you
    # need to get the integer part of the str
    # and convert it to an integer
    # the _ is the character where you can split
    # maxsplit=1 does only split once,
    # so you get two elements back
    # if the _ is missing, split will raise an Exception
    return int(path.stem.split("_", maxsplit=1)[1])


# Use the high level Path object
outputs = Path.home() / "Outputs"
# print(outputs)
# Path: /home/username/Outputs

# use glob for easier search
# rglob is to search recursive
# glob and rglob replicates the shell-syntax
# the wildcard is one * and a ? stands for one character

search = "file_*.txt"
# sorted takes a key argument, which is used to define how it's sorted
# sort_by_int just returns an int and the sorted function
# is using this number to sort
sorted_outputs = sorted(outputs.glob(search), key=sort_by_int)
# the result is a list
# sorted consumes the iterable object and returns a list
# with the sorted elements

# now using this sorted list with Path objects
for path in sorted_outputs:
    # glob does not differe between files, directories or other
    # elements
    # so you need to check, if path is a file
    if path.is_file():
        # if it's a file, then open it
        # the Path onject do have the method open
        # it supports like the open function a context manager
        with path.open() as fd:
            # iterating this file line by line
            # where the line end is not stripped away
            for line in fd:
                # print the line, but tell print not to add an additional
                # line end, because the line has already a line end
                print(line, end="")
                # you can use the stdout to redirect the output
                # in your shell to a file for example or netcat
                # or gzip etc...
Relevant documentation:
Using the program (I named it randomly searchp.py):


Output:
[andre@andre-Fujitsu-i5 ~]$ python3 searchp.py | gzip > output.txt.gz [andre@andre-Fujitsu-i5 ~]$ zcat output.txt.gz 666 123 000
I created the Outputs directory in my home directory and put 3 files in, where each file had only one line with a newline character at the end.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
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
Question [solved] compressing files with python. SpongeB0B 1 658 May-26-2023, 03:33 PM
Last Post: SpongeB0B
  Help replacing word in Mutiple files. (SOLVED) mm309d 0 843 Mar-21-2023, 03:43 AM
Last Post: mm309d
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,180 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  azure TTS from text files to mp3s mutantGOD 2 1,713 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  [SOLVED] [BeautifulSoup] How to get this text? Winfried 6 1,998 Aug-17-2022, 03:58 PM
Last Post: Winfried
  Writing into 2 text files from the same function paul18fr 4 1,690 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,580 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Human Sorting (natsort) does not work [SOLVED] AlphaInc 2 1,146 Jul-04-2022, 10:21 AM
Last Post: AlphaInc
  select files such as text file RolanRoll 2 1,176 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 1,947 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