In Python, the action file object is created using the Open function, and the following table lists the functions that are commonly used for file manipulation:
Serial number
Method and Description
1.file.close ()
Close the file. The file can no longer read and write after closing.
2.file.flush ()
Refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write.
3.file.fileno ()
Returns an integer that is a file descriptor (Descriptor FD Integer) that can be used in some of the underlying operations, such as the Read method of the OS module.
4.file.isatty ()
Returns True if the file is connected to an end device, otherwise False is returned.
5.file.next ()
Returns the next line of the file.
6.file.read ([size])
Reads the specified number of bytes from the file, if none is given or is negative.
7.file.readline ([size])
Reads the entire line, including the "\ n" character.
8.file.readlines ([Sizehint])
Reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizhint because the buffer needs to be populated.
9.file.seek (offset[, whence])
Set the current location of the file
10.file.tell ()
Returns the current location of the file.
11.file.truncate ([size])
Intercepts the file, the truncated byte is specified by size, and the default is the current file location.
12.file.write (str)
Writes a string to the file without a return value.
13.file.writelines (Sequence)
Writes a list of sequence strings to the file and adds a newline character to each line if a line break is required.
WriteFile = open ("D://pythonfile.txt","W")#open a file, if none is createdWritefile.write ("Python is very good \nyes,i think so! ")#writes a paragraph to a file that is overwritten if there is information in the fileWritefile.flush (); Writefile.close ();Print(Writefile.name)#Print the name of the fileReadFile= Open ("D://pythonfile.txt","RB")Print(Readfile.tell ())#Open the current location of the file, because we have not done anything to the file, so the current position is 0, indicating the beginning of the fileReadfile.seek (10,0)#the Seek method of the file object that represents moving to another location in the open filePrint(Readfile.tell ()) Tagdata=readfile.read (10)#The Read method reads a specified number of bytes from the open file and returns a string containing the read dataPrint(Tagdata)
Python3.5 Getting Started learning record-file