-2

I have thousands of .txt files. These text files include one string. (Every file has different string.)

I want to edit these strings but i don't want to manually open each file one-by-one for editing. So i want to merge all these files into a single .txt file and after my editing done, i want to seperate/split them again with the same file names they were owned before i merged.

For example;

i have these text files.

lorem.txt (hi, this is an example line.)

ipsum.txt (hi, this is another line.)

merol123.txt (hi, just another line.)

*

merged.txt >>> edited and ready to split again. >> result needs to be like this;

*

lorem.txt (hi, this is edited line.)

ipsum.txt (another edited line.)

merol123.txt (another edited line. number 4847887)

Note: Sentences inside brackets represents string inside txt file.

Is it possible? I am waiting your helps, thanks!

  • If you simply merge all of them to one file without any additional data, how will you possibly be able to split them back to original names? – Tomerikoo Jul 13 '19 at 00:36
  • Yes i need to store original file name for it too, but i don't know how to do it. It will not be a problem for me. It can store data too! –  Jul 13 '19 at 00:59
  • By the way you are trying to go about this, it will be much (much!) easier, opening the files one by one, changing what you want, and saving all that merging-splitting stuff. I mean, you're saying *i don't want to manually open each file one-by-one for editing* but in order to merge and split you will have to open each twice... – Tomerikoo Jul 13 '19 at 01:05
  • @ipaleka's answer solved my problem. Thanks to him-her again. –  Jul 13 '19 at 01:49
  • Yes @ipaleka 's solution is beautiful. But I have a feeling that if you run timing test with that solution and simply working on each file, the latter will be faster – Tomerikoo Jul 13 '19 at 10:19

1 Answers1

1

First of all, I assumed you've not repeated your strings correctly (like "hi, this is an example line." != "hi, this is edited line.") by mistake, not on purpose (that I can't figure out).

I named the accumulative file common.doc to distinct from the other .txt files in the target directory. Also, this example code implies all the files are in the same directory.

# merging.py
import os
import glob

with open("common.doc", "w") as common:
    for txt in glob.glob("./*.txt"):
        with open(txt, "r") as f:
            content = f.read()
        common.write("{} ({})\n".format(os.path.basename(txt), content))

And after common.doc editing:

# splitting.py
with open("common.doc", "r") as common:
    for line in common:
        name = line[:line.find(" (")]
        text = line[line.find(" (")+2:line.rfind(")")]
        with open(name, "w") as f:
            f.write(text)

And a solution for multiline text (merging stays with .strip() removed on content writing), not suitable for hundreds of thousands of files tho...

# splitting2.py
with open("common.doc", "r") as common:
    everything = common.read()
elements = everything.split(")")
for elem in elements:
    name = elem[:elem.find(" (")].strip()
    text = elem[elem.find(" (")+2:]
    if name:
        with open(name, "w") as f:
            f.write(text)
ipaleka
  • 3,745
  • 2
  • 13
  • 33
  • To read better this comment: http://prntscr.com/oecu48 Thank you but how it will understand to which name the text file be? Let's say the text file named 999.txt has a string like "asd123" and after the merge that string is at 6987th line in the common.doc. I changed that string to "678qwe" How code will know it needs to create that line as "999.txt" again? btw: yes i will change all strings in each file. they will not stay same. maybe they will stay same in few files. –  Jul 13 '19 at 00:41
  • So i think, it needs to store original file name data too. Otherwise how can it understand how to create file name of that string. –  Jul 13 '19 at 01:00
  • EDIT: Working flawless! Thank you. If you read a comment from me about an error few minutes ago it was not your fault, i made a mistake about files. I fixed it now, and working good! Thank you again. I marked as correct answer –  Jul 13 '19 at 01:49
  • By the way, if the file has more than 1 line. Do you have a solution for that? Some files have "blank" lines. They dont have any characters. But i want to make sure i will leave it like it was before. It might harm my project even they are blank lines. –  Jul 13 '19 at 01:54
  • I have 25.000 files and it's working, thank you again –  Jul 13 '19 at 04:58