-1

I have a list of files in a folder, the format of the file name as follows

coronavirus-tweet-id-2020-06-01-00.txt
coronavirus-tweet-id-2020-06-01-01.txt
coronavirus-tweet-id-2020-06-01-02.txt
coronavirus-tweet-id-2020-06-01-00.txt
coronavirus-tweet-id-2020-06-02-01.txt
coronavirus-tweet-id-2020-06-02-02.txt

Inside the file, data is in the format

1279067346301321217
1279067346209038337
1279067346343038977
1279067346301333507

I wanted to merge all these files to one file(according to date) so the final output should look like

coronavirus-tweet-id-2020-06-01.txt
coronavirus-tweet-id-2020-06-02.txt

Then I wanted to collect 1000 sample the content from the final the file.

I am not getting any clue to merge the file.

1 Answers1

0

The answer to this question can be found in some other questions but the jist of it is below.

root_filename = "coronavirus-tweet-id-2020-06-01.txt"

with open(root_filename, "w") as root_file:
    for i in range(0, 99): # or whatever you file quantity is
        with open(f"coronavirus-tweet-id-2020-06-01-{i:02}.txt", "r") as temp:
            root_file.write(temp.read())
  • How can I automate the f"coronavirus-tweet-id-2020-06-01-{i:02}.txt". I wanted to pass the different date here – SUSHMA KUMARI Jul 28 '20 at 18:53
  • Well I guess you could loop the filename. There are plenty of examples out there. Have a look at [f strings](https://realpython.com/python-f-strings/) as well. – Robert Koch Jul 29 '20 at 10:24