0

a bit of a silly question. I am trying to insert (prepend) multiple text files (apple, banana, pear) into a main text file (fruitsalad.txt). How do a make this more concise? (PS there is more fruit than what I am showing!)

input01 = path_include + 'apple.txt'
input02 = path_include + 'banana.txt'
input03 = path_include + 'pear.txt'

prepend01 = open(input01,'r').read()
prepend02 = open(input02,'r').read()
prepend03 = open(input03,'r').read()

open(fruitsalad_filepath, 'r+').write(prepend01+prepend02+prepend03 + open(fruitsalad_filepath).read())
Andreuccio
  • 1,053
  • 2
  • 18
  • 32
  • See http://stackoverflow.com/questions/4454298/prepend-a-line-to-an-existing-file-in-python#4454522 for why there's no fundamentally easier way to achieve this. Also consider the comment below that question that suggests using a temporary file in order not to risk data loss on crashes. – Wisperwind Mar 08 '17 at 16:40

4 Answers4

1

Assuming you have some list

fruit = ['apple.txt', 'banana.txt', 'pear.txt']

you can open the target file, then write the contents of each fruit file across one at a time

with open(fruitsalad_filepath, 'w+') as salad:
    for item in fruit:
        with open(path_include+item) as f:
            salad.write(f.read())

Doing it this way means you don't have to save the text in intermediate variables, which could eat up a lot of memory. Also, you should read up on the use of context managers in python (the with ... as ... : statements)

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • I've actually realised that my question was incomplete, leading to an inaccurate answer. At the time of appending, the file `salad` already contains data that should not be overwritten, but pre-prended by the content of the list `fruit` – Andreuccio Mar 13 '17 at 18:45
  • @Andreuccio Then you need to read that data into memory before the loop I wrote, then write it at the end of the file after the loop. There isn't a way to write to the beginning of a file without writing over existing information. – Patrick Haugh Mar 13 '17 at 20:13
0

You could use glob (https://docs.python.org/2/library/glob.html) to include everything in a for loop. Then you could put all your input's content in a string. Also, I would use with so that you don't have to worry about file handlers.

import glob

prepend_text = ""

for input in glob.glob("%s*.txt" % (path_include)):
   with open(input, 'r') as f:
      prepend_text += f.read()

with open(fruitsalad_filepath, 'r') as f:
   prepend_text += f.read()

with open(fruitsalad_filepath, 'w') as f:
   f.write(prepend_text)

Note that this code assumes that fruitsalad_filepath is NOT in path_include. If it is, then you'll have to add some checks (and remove the last read).

Mikk
  • 804
  • 8
  • 23
0

It should be something like this:

import codecs
# for example you prepared list of .txt files in folder
# all_files - list of all file names
all_files = [] 
# content from all files
salad_content = ''

for file_name in all_files:
    # open each file and read content
    with codecs.open(file_name, encoding='utf-8') as f:
        salad_content += f.read()

# write prepared content from all files to final file
with codecs.open(fruitsalad_filepath, 'w', 'utf-8') as f:
    f.write(salad_content)

For finding .txt files in the folder you can use this approach.

Community
  • 1
  • 1
Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
0

While, instead of opening every files, try to use the offical libraray fileinput, and then you can open multi files together in iterator way, as you can see the function:

fileinput.input([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]])

Menglong Li
  • 2,177
  • 14
  • 19