1, file operations.
Model housewife nurse teacher .txt
. 1, the file path: d: \ Model housewife nurse teacher .txt
2, encoding: utf-8 gbk. . . .
3, Operation: read-only, write-only, append, read, write, read .....
what encoding the file is stored, it is open to any coding operation.
Model housewife nurse teacher .txt
. 1, the file path: d: \ Model housewife nurse teacher .txt
2, encoding: utf-8 gbk. . . .
3, Operation: read-only, write-only, append, read, write, read .....
what encoding the file is stored, it is open to any coding operation.
Read-only: R & lt
RB
F = Open ( 'Model class housewife nurses', MODE = 'R & lt', encoding = 'UTF-. 8')
Content reached, f.read = ()
Print (Content, type (Content))
f.close ( )
# F = open ( 'Model class housewife nurses', MODE = 'RB',)
# reached, f.read Content = ()
# Print (Content)
# f.close ()
# reached, f.read Content = ()
# Print (Content)
# f.close ()
write r +
r + B read (in bytes type)
# = F Open ( 'log', MODE = 'r +', encoding = 'UTF-. 8')
# Print (reached, f.read ())
# f.write ( 'big Meng Meng')
# f.close ()
r + B read (in bytes type)
# = F Open ( 'log', MODE = 'r +', encoding = 'UTF-. 8')
# Print (reached, f.read ())
# f.write ( 'big Meng Meng')
# f.close ()
f = open('log',mode='r+b')
print(f.read())
f.write('大猛,小孟'.encode('utf-8'))
f.close()
print(f.read())
f.write('大猛,小孟'.encode('utf-8'))
f.close()
Write only: w
wb
content # source file first clear, in writing.
Open F = # ( 'log', MODE = 'W', encoding = 'UTF-. 8')
# f.write ( 'seen near similar disputes')
# f.close ()
wb
content # source file first clear, in writing.
Open F = # ( 'log', MODE = 'W', encoding = 'UTF-. 8')
# f.write ( 'seen near similar disputes')
# f.close ()
f = open('log',mode='wb')
f.write('附近看到类似纠纷'.encode('utf-8'))
f.close()
w+
# f = open('log',mode='w+',encoding='utf-8')
# f.write('aaa')
# f.seek(0)
# print(f.read())
# f.close()
w+b
.......
f.write('附近看到类似纠纷'.encode('utf-8'))
f.close()
w+
# f = open('log',mode='w+',encoding='utf-8')
# f.write('aaa')
# f.seek(0)
# print(f.read())
# f.close()
w+b
.......
追加
# f = open('log',mode='a',encoding='utf-8')
# f.write('佳琪')
# f.close()
# f = open('log',mode='ab')
# f.write('佳琪'.encode('utf-8'))
# f.close()
# f.write('佳琪'.encode('utf-8'))
# f.close()
Absolute path #
# f = open ( 'd: \ Model class housewife nurse .txt', MODE = 'R & lt', encoding = 'UTF-. 8')
# reached, f.read Content = ()
# Print (Content)
# F. close ()
# f = open ( 'd: \ Model class housewife nurse .txt', MODE = 'R & lt', encoding = 'UTF-. 8')
# reached, f.read Content = ()
# Print (Content)
# F. close ()
---- #Bytes in> STR
# F = Open ( 'Model class housewife nurses', MODE = 'R & lt', encoding = 'UTF-. 8')
# reached, f.read Content = ()
# f.write ( 'fjsdlk' )
# f.close ()
# F = Open ( 'Model class housewife nurses', MODE = 'R & lt', encoding = 'UTF-. 8')
# reached, f.read Content = ()
# f.write ( 'fjsdlk' )
# f.close ()
# F = open ( 'Model class housewife nurses', MODE =' RB ',)
# reached, f.read Content = ()
# Print (Content)
# f.close ()
# f = open (' log ', MODE =' + R & lt ', encoding =' UTF-. 8 ')
# Print (reached, f.read ())
# f.close ()
# reached, f.read Content = ()
# Print (Content)
# f.close ()
# f = open (' log ', MODE =' + R & lt ', encoding =' UTF-. 8 ')
# Print (reached, f.read ())
# f.close ()
# f = open('log',mode='r+b')
# print(f.read())
# f.write('大猛,小孟'.encode('utf-8'))
# f.close()
# print(f.read())
# f.write('大猛,小孟'.encode('utf-8'))
# f.close()
For # w: This file will not create a file
# F = Open ( 'log', MODE = 'W', encoding = 'UTF-. 8')
# f.write ( 'cavalry infantry')
# f.close ()
# F = Open ( 'log', MODE = 'W', encoding = 'UTF-. 8')
# f.write ( 'cavalry infantry')
# f.close ()
# Source file contents first clear, in writing.
Open F = # ( 'log', MODE = 'W', encoding = 'UTF-. 8')
# f.write ( 'seen near similar disputes')
# f.close ()
Open F = # ( 'log', MODE = 'W', encoding = 'UTF-. 8')
# f.write ( 'seen near similar disputes')
# f.close ()
# f = open('log',mode='w+',encoding='utf-8')
# f.write('aaa')
# f.seek(0)
# print(f.read())
# f.close()
Open F = # ( 'log', MODE = 'WB')
# f.write ( 'seen near similar disputes' .encode ( 'UTF-. 8'))
# f.close ()
# f = open('log',mode='a',encoding='utf-8')
# f.write('佳琪')
# f.close()
#
# f = open('log',mode='a',encoding='utf-8')
# f.write('佳琪')
# f.close()
# f.write('佳琪')
# f.close()
#
# f = open('log',mode='a',encoding='utf-8')
# f.write('佳琪')
# f.close()
# f = open('log',mode='a+',encoding='utf-8')
# f.write('佳琪')
# f.seek(0)
# print(f.read())
# f.close()
# f.write('佳琪')
# f.seek(0)
# print(f.read())
# f.close()
# f = open('log',mode='ab')
# f.write('佳琪'.encode('utf-8'))
# f.close()
# Detailed features
Open obj = # ( 'log', MODE = 'R & lt +', encoding = 'UTF-. 8')
# reached, f.read Content = (. 3) are read out character #
# f.seek (3) # accordance with the word section set the cursor position
# f.tell () to tell you the cursor position
# Print (f.tell ())
# f.read Content = ()
# Print (Content)
# f.tell ()
# f.readable () # whether scale
# line = f.readline () # read line by line
# line = f.readlines () # each line as an element in the list is added to the list
# f.truncate (. 4)
# for line F in :
# Print (Line)
# f.close ()
# reached, f.read Content = (. 3) are read out character #
# f.seek (3) # accordance with the word section set the cursor position
# f.tell () to tell you the cursor position
# Print (f.tell ())
# f.read Content = ()
# Print (Content)
# f.tell ()
# f.readable () # whether scale
# line = f.readline () # read line by line
# line = f.readlines () # each line as an element in the list is added to the list
# f.truncate (. 4)
# for line F in :
# Print (Line)
# f.close ()
# f = open('log',mode='a+',encoding='utf-8')
# f.write('佳琪')
# count = f.tell()
# f.seek(count-9)
# print(f.read(2))
# f.close()
# f.write('佳琪')
# count = f.tell()
# f.seek(count-9)
# print(f.read(2))
# f.close()
# with open('log',mode='r+',encoding='utf-8') as f,\
# open('log',mode='w+',encoding='utf-8') as f1:
# open('log',mode='w+',encoding='utf-8') as f1:
the INPUT = username ( ' Please enter a user name you want to register: ' ) password = the INPUT ( ' Please enter the password you want to register: ' ) with open('list_of_info',mode='w',encoding='utf-8') as f: f.write ( ' {} \ {} the n- ' .format (username, password)) Print ( ' Congratulations, successfully registered ' ) lis = [] i = 0 while i < 3: USN = the INPUT ( ' Please enter your user name: ' ) pwd = the INPUT ( ' Please enter your password: ' ) with open('list_of_info',mode='r+',encoding='utf-8') as f1: for line in f1: lis.append(line) IF USN == LIS [0] .strip () and pwd == LIS [1 ] .strip (): Print ( ' Login successful ' ) BREAK the else : Print ( ' account and password error ' ) i+=1
#str ---> byte encode encoding
# s = 'brother'
# B = s.encode ( 'UTF-. 8')
# Print (B)
# #byte ---> STR decode decoded
# s1 = b.decode ( 'UTF-. 8')
# Print (S1)
# s = 'brother'
# B = s.encode ( 'UTF-. 8')
# Print (B)
# #byte ---> STR decode decoded
# s1 = b.decode ( 'UTF-. 8')
# Print (S1)
# s = 'abf'
# b = s.encode('utf-8')
# print(b)
# #byte --->str decode 解码
# s1 = b.decode('gbk')
# print(s1)
Review content
# File
# open file
#open ( 'path', 'Open', 'specify the encoding')
# Open W + A + R & lt RWA B +
# + R & lt open file read and write directly write
# encoding - utf- 8
# operation file
# read
# read a one-time reading
# readlines one-time reading
# readline read line by line
# does not know where the end of the
# video pictures rb bytes in bytes read
# for loop - the best! ! !
# Write
# the Write
# cursor - file pointer
#seek _ Move the cursor to a specified location
#tell _ get the current cursor position
#truncate _ intercepted file
# close the file
#close
# open file
#open ( 'path', 'Open', 'specify the encoding')
# Open W + A + R & lt RWA B +
# + R & lt open file read and write directly write
# encoding - utf- 8
# operation file
# read
# read a one-time reading
# readlines one-time reading
# readline read line by line
# does not know where the end of the
# video pictures rb bytes in bytes read
# for loop - the best! ! !
# Write
# the Write
# cursor - file pointer
#seek _ Move the cursor to a specified location
#tell _ get the current cursor position
#truncate _ intercepted file
# close the file
#close
# Modify the file
with open ( 'Nurse teacher', encoding = 'utf-8 ') as f, open ( ' class Nurse .bak', 'W', encoding = 'UTF-. 8') AS F2:
for Line f in:
IF 'star child' in line: # teacher: child star
line = line.replace ( 'star children', 'ah Johnson')
# write file
f2.write (line) # nurse: Golden boss
with open ( 'Nurse teacher', encoding = 'utf-8 ') as f, open ( ' class Nurse .bak', 'W', encoding = 'UTF-. 8') AS F2:
for Line f in:
IF 'star child' in line: # teacher: child star
line = line.replace ( 'star children', 'ah Johnson')
# write file
f2.write (line) # nurse: Golden boss
os Import
os.remove ( 'Nurse teacher') # delete files
os.rename ( 'Nurse teacher .bak', 'Nurse teacher') # renaming files
os.remove ( 'Nurse teacher') # delete files
os.rename ( 'Nurse teacher .bak', 'Nurse teacher') # renaming files