Home > other >  Beginner's python
Beginner's python

Time:09-28

I'm self-study "stupid way to learn python", meet a question that out_file=open (to_file, 'w'). The write (open (from_file). The read ()), why can't this string of code execution out_file. Close? You need to perform. The close? Why is that?

CodePudding user response:

Need to close, but when you return here out_file write the return value is an int

Can take close to this
 
Fw=open (to_file, 'w')
Fr=open (from_file)
Fw. Write (fr. Read ())
Fr. Close ()
Fw. Close ()


But generally used with open file in python, use with don't close the
 
With the open (to_file, 'w') as fw, open (from_file) as fr:
Fw. Write (fr. Read ())

CodePudding user response:

reference 1/f, ice all over the sky the wind response:
needs to be close, but when you return here out_file write the return value is an int

Can take close to this
 
Fw=open (to_file, 'w')
Fr=open (from_file)
Fw. Write (fr. Read ())
Fr. Close ()
Fw. Close ()


But generally used with open file in python, use with don't close the
 
With the open (to_file, 'w') as fw, open (from_file) as fr:
Fw. Write (fr. Read ())

Reply thank god, is like that, I was using "python3 stupid way to learn the book" self-study, there is a problem sets, the original script is step by step to write scripts to 10, the last two lines as well as you write, need to use. The close, the author can write 10 lines of code to say, is that I wrote that line, but in the end the authors do not add. The close statement, so I'm a little don't understand, why can not added, after a short

CodePudding user response:

Python does not perform in the close operation, the file by default is close, so the running script is small, does not perform the close issue also is not big, but the default close usually wait to garbage collection when, just released automatically, if it is in service, may lead to resources transition is used, resulting in abnormal service, so it safer to carry out close operation,

CodePudding user response:

Thank you for the great god

CodePudding user response:

From here, you can see that you do not understand what the object is, here, after the open file object, but after you write back, return is not a file object, but after the write return values, so can't close, if is a beginner, suggest to write step by step, so few mistakes, if you are familiar with, understand, can be strung together to write, but it is also not recommended ah, such a later time, see the code, or transfer to other colleagues, it's not convenient

CodePudding user response:

reference 5 floor roylam12345 reply:
from here, you can see that you do not understand what the object is, here, after the open file object, but after you write back, return is not a file object, but after the write return value, so can't close, if is a beginner, suggest to write step by step, so few mistakes, if you are familiar with, understand, can be strung together to write, but it is also not recommended ah, such a later time, see the code, or transfer to other colleagues, very inconvenient

Thank you very much, I am a beginner, code written in a is the author to the homework problems, so I will try to write, you write very clear, colloquial, thank you
  • Related