file object = open(file_name [, access_mode][, buffering])
file_name:file_name A variable is a string value that contains the name of the file you want to access .
access_mode:access_mode Determines the mode of opening files : read-only , write in , Supplemental . All available values are shown in the complete list below . This parameter is optional , The default file access mode is read-only (r).
buffering: If buffering The value of is set to 0, There will be no deposit . If buffering Value of 1, When you access a file, you will deposit lines . If you will buffering The value of is set to be greater than 1 The integer of , It shows that this is the buffer size of the deposit area . If you take a negative value , The buffer size of the deposit area is the system default .
with open('filepath','r') as f:
for line in f:
print(line)
print(' A line of data ')
although f Is a file instance , But you can cycle through each row in the above way , Each line is a string when processing str, And this is the fastest and simplest way .
fileObject.read([count])
The parameter passed is the count of bytes to read from the opened file . This method reads in from the beginning of the file , If no incoming count, It will try to read as much as possible , Probably to the end of the file .
with open('filepath','r') as f:
ff=f.read()
This function reads the entire contents of the file into a string at one time . It's the kind of lump , If you put ff Output by loop reading , It will be a character , because ff Is string , Essentially, tuple.
with open('filepath','r') as f:
lines=f.readlines()
for line in lines:
print(line)
with open('filepath','r') as f:
line =f.readline()
while line:
print(line)
line=f.readline()
This is a line by line reading , Very memory saving , It's good when the file is huge .
write() Method to write any string to an open file . It's important to pay attention to ,Python Strings can be binary data , Not just words .
fileObject.write(string)
# Open a file
fo = open("foo.txt", "w")
fo.write( "\nVery good site!\n")
# Close open files
fo.close()
File Object's close() Method to refresh any information in the buffer that has not been written , And close the file , After that, no more writes can be made .
fileObject.close()
# Open a file
fo = open("foo.txt", "w")
print " file name : ", fo.name
# Close open files
fo.close()
tell() Method tells you the current location in the file , let me put it another way , The next read and write will happen after so many bytes at the beginning of the file .
seek(offset [,from]) Method to change the location of the current file .Offset Variable represents the number of bytes to move .From Variable to specify the reference location to start moving bytes .
If from Is set to 0, This means that the beginning of the file is used as a reference location for moving bytes . If it is set to 1, Then use the current position as the reference position . If it's set to 2, Then the end of the file will be used as a reference location .
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10)
print " The string read is : ", str
# Find the current location
position = fo.tell()
print " Current file location : ", position
# Relocate the pointer to the beginning of the file again
position = fo.seek(0, 0)
str = fo.read(10)
print " Reread string : ", str
# Close open files
fo.close()
If you have any questions , Please leave a message .