Initial knowledge of file operation
Create a TXT file for "Nurse stewardess student young woman Contact" in D disk
path file paths: d:\ nurse stewardess student young woman contact information. txt
Encoding encoding method: UTF-8,GBK ....
Mode operation mode: Read Only, write, append, read and write, read .....
# F2 = open (' nurse student stewardess teacher-in-Charge ', encoding='utf-8') # Print (F2.read ()) # f1.close ()
Path: Absolute path: Files are always found starting from the root directory.
relative path: The file found at the beginning of the current directory.
Error Reason analysis:
unicodedecodeerror: ' utf-8 ' codec can ' t decode byte 0xbf in position 0:invalid start byte
encoding Error: File storage is encoded in a manner inconsistent with when the file is opened.
syntaxerror: (Unicode error) ' Unicodeescape ' codec can ' t decode bytes in position 2-3: truncated \uxxxx escape
Two kinds:
The first type: plus R
f1 = open (R ' d:\u nurse stewardess student young woman contact method. txt ', encoding= ' GB2312 ', mode= ' R ')
print (F1.read ())
f1.close ()
The first type: plus \
f1 = open (' d:\\ nurse stewardess student young woman contact Way '. txt ', encoding= ' GB2312 ', mode= ' R ')
print (F1.read ())
f1.close ()
#凡是带b不用encoding
#只有是r模式 mode= ' R ' can be omitted without writing
Common methods of operation
R,w,a
Read mode:
*r
Read
# 1,f.read () read them all. # f2 = open (' Nurse student stewardess teacher-in-charge ', ' encoding= ' utf-8 ')# print (F2.read ()) # f2.close ()
Read (n)
# f1 = open (' The Nurse student stewardess teacher-in-charge ', encoding= ' utf-8 ') # Print (F1.read (3)) # f1.close () # the R mode: Read (n) n is read by character. # RB mode: Read (n) n is reading in bytes.
ReadLine ()
# f1 = open (' The Nurse student stewardess teacher-in-charge ', encoding= ' utf-8 ') # # Print (F1.readline ()) # # Print (F1.readline ()) # # Print (F1.readline ()) # # Print (F1.readline ()) # f1.close ()
ReadLines ()
# f1 = open (' The Nurse student stewardess teacher-in-charge ', encoding= ' utf-8 ') # print (F1.readlines ()) # For line in F1.readlines (): # Print (line)# f1.close ()
For Loop Best
# f1 = open (' The Nurse student stewardess teacher-in-charge ', encoding= ' utf-8 ') # For line in F1: # Print (line)# f1.close ()
Rb
# f1 = open (' The Nurse student stewardess teacher-in-charge ', mode= ' RB ') # Print (F1.read (1)) # f1.close ()
*r+
# f1 = open (' Nurse student stewardess teacher-in-charge ', ' encoding= ' utf-8 ', mode= ' r+ ') # print (F1.read ()) # f1.write (' 666 ') # f1.close ()
R+b
# f1 = open (' The Nurse student stewardess teacher-in-charge ', mode= ' r+b ') # print (F1.read ()) # f1.write (' 666 '. Encode (' Utf-8 ')) # f1.close ()
Write mode:
*w
# f1 = open (' Log1 ', encoding= ' utf-8 ', mode= ' W ') # f1.write (' Old boy is the best training school ... ') # f1.close () # f1 = open (' Log1 ', encoding= ' utf-8 ', mode= ' W ') # f1.write (' French Import Classification results ') # f1.close ()
W: No file, create file write content.
W: If there is a file, empty the original file and write the new content.
Wb
# f1 = open (' log2 ', mode= ' WB ') # f1.write (' French import classification results '. Encode (' Utf-8 ')) # f1.close ()
w+
# f1 = open (' Log1 ', encoding= ' utf-8 ', mode= ' w+ ') # f1.write (' Old and old boy ... ') # f1.seek (0) # adjust cursor # print (F1.read ())# f1.close ()
W+b
Append mode
*a
A: No files, create files to write content.
A: If there is a file, the last side appends new content.
# f1 = open (' Log3 ', encoding= ' utf-8 ', mode= ' a ') # f1.write (' Barry ') # f1.close ()
Ab
# f1 = open (' Log3 ', mode= ' ab ') # f1.write (' China ' encode (' GBK ')) # f1.close ()
A +
# f1 = open (' Log3 ', encoding= ' utf-8 ', mode= ' A + ') # F1.write (' Edwin van der Sar tools for quick development ') # f1.seek (0) # print (F1.read ()) # f1.close ()
A+b
Add:
Read and write #ps non-text files
# f1 = open (' 11.jpg ', mode= ' RB ') # content = F1.read () # Print (content) # f1.close () # F2 = open (' piggy page jpg ', mode= ' WB ') # f2.write (content)
GBK Utf-8
For letters, numbers, and special characters, the code is quoted as ASCII, so it can be converted directly.
S1 = ' 123abc* '
B1 = S1.encode (' Utf-8 ')
S2 = B1.decode (' GBK ')
Print (s2)
05, common methods of file operation
# readlable () writeable () to determine whether readable or writable
# f1 = open (' Log1 ', encoding= ' utf-8 ') # print (f1.readable ()) # print (f1.writable ()) # f1 = open (' Log1 ', encoding= ' utf-8 ', mode= ' r+ ') # print (f1.readable ()) # print (f1.writable ())
# Seek: Adjust the cursor according to bytes
# f1 = open (' Log1 ', encoding= ' utf-8 ') # F1.seek (3) # print (F1.read ()) # f1.close ()
# tell me where the cursor is
# f1 = open (' Log1 ', encoding= ' utf-8 ') # print (F1.tell ()) # print (F1.read ()) # print (F1.tell ()) # f1.close ()
#truncate intercepts the original file, in bytes, only in a mode.
# f1 = open (' Log1 ', encoding= ' utf-8 ', mode= ' a ') # f1.truncate (3) # f1.close ()
#文件操作的另一种写法
# with open () as without active shutdown f1.close ()
# Multiple files can be manipulated with the same open
#with open (' Log1 ', encoding= ' utf-8 ') as F1:#print (F1.read ())#with open (' Log1 ', encoding= ' Utf-8 ') as f1,\#open (' log2 ', encoding= ' utf-8 ', mode= ' W ') as F2:#print (F1.read ())#f2.write (' 666 ')#with open (' Log1 ', encoding= ' utf-8 ') as F1:#content = F1.read ()#f1.close ()#Pass#With open (' Log1 ', encoding= ' utf-8 ', mode= ' W ') as F2:#f2.write (' 333 ')
06, file changes.
1, open the original file Old_file, read the original content to memory.
2, create a new file new_file.
3, the original content through your rewriting to form new content, write to the new file.
4, delete the original file.
5. Rename the new file to the original file.
#方法一, the original file content is not hit, you can use this method, but this method is still very low.
# Import OS # with open (' Change ', encoding= ' Utf-8 ') as f1,\ # Open (' Change.bak ', encoding= ' utf-8 ', mode= ' W ') as F2:# old_content = F1.read () # new_content = Old_content.replace (' Alex ', ' SB ')# f2.write (new_content) # os.remove (' change ')# os.rename (' Change.bak ', ' Change ')
# method 2
ImportOswith Open (' Change', encoding='Utf-8') as F1, open ('Change.bak', encoding='Utf-8', mode='W') as F2: forLineinchF1:new_line= Line.replace ('SB','Alex') F2.write (new_line) os.remove (' Change') Os.rename ('Change.bak',' Change')
Python Learning D8 file Operations Common methods of File modification