python - document processing

File handling:

First, the model parameters

t: text mode (the default).

b: binary mode.

+: Open a file is updated (read and write).

r: open the file in read-only mode. Pointer file will be placed at the beginning of the file. This is the default mode.

rb: open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as pictures and so on.

r +: open a file for reading and writing. The file pointer will be placed at the beginning of the file.

rb +: open a file for reading and writing binary format. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as pictures and so on.

w: only opens a file for writing.

If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.

wb: Open a file for writing in binary format only.

If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted.

If the file does not exist, create a new file. Generally used for non-text files such as pictures and so on.

w + open a file for reading and writing. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.

wb + opens a file for reading and writing binary format. If the file already exists then open the file and start editing from the beginning,

That is the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as pictures and so on.

Open a file for append. If the file already exists, the file pointer will be placed at the end of the file.

In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.

 

Second, the file to be processed commonly used functions

open (): open the file (the file path can be a relative path, or an absolute path).

read (): Reads a specified number of characters from the current location of the file, if not given or is negative, then read all the contents.

readline (): read a file from the entire line, including newline \ n (from the current position to the end of the line).

readlines (): read all the lines and returns a list.

tell (): returns the current file pointer position.

seek (): read pointer move the file to a specified location.

write (): writing to a file specified string.

flush (): refresh buffer, the data buffer is written to the file immediately, while empty the buffer.

close (): Closes an open file. You can not read and write operations after the file is closed.

 

-------------------------------- # 
# Read file
# ------------ --------------------
# open () to open the file and returns a file object
F = open ( 'ch13_test.txt', 'R & lt +')
# = F open (r'D: \ path \ ch13_test.txt ')
Print ( "file name is:", F.Name)

# the read () reads a specified number of characters from a file, if not given or is negative, then read All
# 5 characters from start to read the file header
Print (f.read (5))

# eight characters continue reading
Print (f.read (8))

# readline () to read the entire line from the file
print (f .readline ()) # from the current position to the end of the line (including line feed)
Print (f.readline (25)) # read the specified number of characters (remaining characters if the current line is not enough, the end of the case of line breaks)
Print (F .readline ())

# negative, the entire contents of the file read () from the current cursor position to the end of the file
Print (reached, f.read (-1))

# Get the current document position
POST = f.tell ()
Print ( "current file location: ", POST)

# the Seek (offset [,The whence]) move the file to a specified location read pointer
# reset the read pointer to the beginning of the file
f.seek (0, 0) # whence 0: Header; 1: a current location; 2: End of File

POST = f.tell ()
Print ( "current document position is:", POST)

# the readlines () Read all row and returns a list of
I =. 1
for line in f.readlines ():
Print (.. "{} {}" the format (I, line), End = "")
I =. 1 +

# Close () Close an opened file
f.close ()


-------------------------------- # 
# write to file
# ------------ --------------------
# open a file for reading and writing (the file exists, open; does not exist, new)
F = open ( 'ch13_test2.txt', '+ W ')
Print ( "file name is:", F.Name)

# locate the end of the file
# f.seek (0, 2)

str1 = "the If you want to GET something \ nYou have have to give \ nYou have have to!! Learn, to insist! \ the n-"
# write a string
f.write (str1)

# write to file a sequence string list
str_list = [" If you really think it's hard for you. \ n "," Then you quit. \ the n-",
" Once you quit. \ the n-"," Never complain. \ the n-"]
f.writelines (str_list)

# relocated to the beginning of the file
f.seek (0, 0)

# read the entire contents
Print (f.read ())

# flush the buffer
f.flush ()
# close the file
f.close ()

Open file for append again #
F = Open ( 'ch13_test2.txt', '+ A')
f.write ( "Bye!")

F.seek (0, 0)
Print (reached, f.read ())
f.close ()

 

Guess you like

Origin www.cnblogs.com/Teachertao/p/11241461.html