Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

Python file open/close every time vs keeping it open until the process is finished

I have about 50 GB of text file and I am checking the first few characters each line and writing those to other files specified for that beginning text.

For example. my input contains:

cow_ilovecow
dog_whreismydog
cat_thatcatshouldgotoreddit
dog_gotitfromshelter
...............

So, I want to process them in cow, dog and cat (about 200) categories so,

if writeflag==1:
    writefile1=open(writefile,"a") #writefile is somedir/dog.txt....
    writefile1.write(remline+"
")
    #writefile1.close()

so, what is the best way, should I close? Otherwise if I keep it open, is writefile1=open(writefile,"a") doing the right thing?

question from:https://stackoverflow.com/questions/11349020/python-file-open-close-every-time-vs-keeping-it-open-until-the-process-is-finish

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should definitely try to open/close the file as little as possible

Because even comparing with file read/write, file open/close is far more expensive

Consider two code blocks:

f=open('test1.txt', 'w')
for i in range(1000):
    f.write('
')
f.close()

and

for i in range(1000):
    f=open('test2.txt', 'a')
    f.write('
')
    f.close()

The first one takes 0.025s while the second one takes 0.309s


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...