简体   繁体   中英

Better approach for reading/writing files in python?

Suppose I have a file (say file1.txt ) with data around 3mb or more. If I want to write this data to a second file (say file2.txt ), which one of the following approaches will be better?

Language used: Python 2.7.3

Approach 1 :

file1_handler = file("file1.txt", 'r')
for lines in file1_handler:
    line = lines.strip()
    # Perform some operation
    file2_handler = file("file2.txt", 'a')
    file2_handler.write(line)
    file2_handler.write('\r\n')
    file2_handler.close()
file1_handler.close()

Approach 2 :

file1_handler = file("file1.txt", 'r')
file2_handler = file("file2.txt", 'a')
for lines in file1_handler:
    line = lines.strip()
    # Perform some operation
    file2_handler.write(line)
    file2_handler.write('\r\n')
file2_handler.close()
file1_handler.close()

I think approach two will be better because you just have to open and close file2.txt once. What do you say?

Use with , it will close the files automatically for you:

with open("file1.txt", 'r') as in_file, open("file2.txt", 'a') as out_file:
    for lines in in_file:
        line = lines.strip()
        # Perform some operation
        out_file.write(line)
        out_file.write('\r\n')

Use open instead of file , file is deprecated.

Of course it's unreasonable to open file2 on every line of file1.

I was recently doing something similar (if I understood you well). How about:

file = open('file1.txt', 'r')
file2 = open('file2.txt', 'wt')

for line in file:
  newLine = line.strip()

  # You can do your operation here on newLine

  file2.write(newLine)
  file2.write('\r\n')

file.close()
file2.close()

This approach works like a charm!

My solution (derived from Pavel Anossov + buffering):

dim = 1000
buffer = []
with open("file1.txt", 'r') as in_file, open("file2.txt", 'a') as out_file:
    for i, lines in enumerate(in_file):
        line = lines.strip()
        # Perform some operation
        buffer.append(line)
        if i%dim == dim-1:
            for bline in buffer:
                out_file.write(bline)
                out_file.write('\r\n')
            buffer = []

Pavel Anossov gave the right solution first: this is just a suggestion ;) Probably it exists a more elegant way to implement this function. If anyone knows it, please tell us.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM