Home > other >  Consult a great god about why use in Python close will be prompted to do not need?
Consult a great god about why use in Python close will be prompted to do not need?

Time:10-12


When I put the copy of the statement is simplified into two lines, then use the close statement will prompt is as follows:


Thank the first, baidu did not find the answer

CodePudding user response:

In_file is string
Out_file the return value is written to the file,

Is not the file pointer, so they have no close method,

Write code, to understand the role of the code, use function, to understand arguments and return values,

CodePudding user response:

An overview of the
Write () method is used to write the specified string file,

In front of the file before closing or buffer, string content stored in the buffer, then you can't see the written content in the file is,

If file open mode with b, then write to the file content, STR (parameters) to encode method into bytes form, otherwise an error: TypeError: a bytes - like the object is required, not 'STR'

Syntax
Write () method syntax is as follows:

FileObject. Write ((STR))
Parameter
STR - string to write to the file,

The return value
Returns the written characters in length,

CodePudding user response:

Wrong is that the write method is the return value is of type int, the int type assigned to the out_file variables, so just can have an int type without close method error,
Not you think out_file variable is equal to the open () function returns the handle, and then use this handle to perform the write method

CodePudding user response:

` write ` the return value is an integer, there is no close property

CodePudding user response:

Object of type int not close () method, says is very clear, is the wrong type
  • Related