Review 09 file operations

Review the lesson # 
# File
# open file
#open ( 'path', 'Open', 'specify the encoding')
# Open W + A + R & lt RWA RB + WB + + + ab &
# recommended that only one operation. When read only write read and write only when additional time is added only
# operation file
# read
#read one-time # read file is too large easily burst
#readline line by line read # # do not know where the end of the video image type in accordance with the type of byte read can not read in rows
# memory is full-time reading will easily burst memory #readlines,

#for cycle time is the best way!
# Write
#write line method to write write write directly newline \ the n-
# ==== professional cursor called the file pointer
#seek move the cursor to a specified location
#tell get the current cursor location
#truncate intercept file
# modified file
# file is use a special method can not be modified to modify the
# file is modified #open operating system file system is built-in functions instead of python function
# Default operating system is GBK encoding all encoding must not specify omitted encoding
modified # file read a write a puncturing a rename
with open ( 'log', encoding = 'utf-8') as f, open ( " log.bak "," w "encoding =" UTF-8 ") AS f2:
for i in f:
IF 'things' in i:
i = i.replace (" things "," strange ")
# write file
f2 .write (i)
# delete files and rename files
Import os
os.remove ( 'log') # delete files
os.rename ( 'log.bak', 'log ') # renaming files

Guess you like

Origin www.cnblogs.com/yexingyi/p/11183082.html
Recommended