4 basic grammar file processing python

 

1. What is file
a utility operating system to your hard drive operations

 

2, why should file
because humans and computers to permanently store data

 

3, how to use file

 

Relative path: a.txt # must be at the same level currently py file directory
absolute path: D: \ project path \ python13 of \ day07 \ a.txt

 

 

Reading file

Open = F ( ' a.txt ' ) 

Print (f.readable ()) # determines whether the current file is readable True 
Print (f.writable ()) # determines whether the current file is writable False 

f.close ()    # recovery operation system resources
# \ Wrap may be implemented as the following simultaneous read and write operations relative path 
with Open ( ' a.txt ' , MODE = ' R & lt ' ) AS RF, \ 
    Open ( ' a.txt ' , MODE = ' W ' ) AS WF:     # with will automatically help you recover the operating system resources without their own operations, close the 
    Print (rf.readable ())     # True 
    Print (rf.writable ())     # False 

# r escape the absolute path 
with open (r ' D: \ oldboy_edu \ python_code \ Day7 \ a.txt ' , encoding = ' UTF-. 8 ' ) AS F:     #Without encoding = 'utf-8' garbled, unexplained 
    Print (reached, f.read ())      # read the file

th.jpg (raw material) lixiaoran.png (generated images)

# Default to open the file mode: rt read the same text and r 
# rb the Read bytes 
with Open ( ' a.txt ' , the MODE = ' rb ' ) AS f:        # contents: Nice weather 
    Print (f.read () )
 # b '\ XE4 \ xbb \ x8a \ xe5 \ Xa4 \ xA9 \ xe5 \ Xa4 \ xA9 \ XE6 \ XB0 \ X94 \ XE7 \ x9c \ x9F \ xe5 \ xa5 \ XBD' 

# read jpg images to binary, and then the binary data is written to png file, create a new picture 
with Open (r ' th.jpg ' , the MODE = ' rb ' ) AS the RF, \ 
        Open ( ' lixiaoran.png ' , the MODE = ' wb ' ) AS WF: 
    lixiaoran =rf.read ()
     Print (lixiaoran) 
    wf.write (lixiaoran)

Open files three modes:
R & lt: 1, ROM 2, if the file does not exist, an error will
w :( caution) 1, 2 write-only, if the file does not exist, a new data file 3 is written, if the file in the data memory, the data will be cleared, rewritten
a: 1, 2 additional write, if the file exists in the data, the data is appended at the end of the existing data 3, if the file does not exist, create a new file write data
processing schema files:
t #txt mode
b # binary mode
. "" "

Open with (R & lt ' the dir \ b.txt ' , ' R & lt ' , encoding = ' GBK ' ) AS F:
     Print (f.readable ())
 Print (reached, f.read ())
 Print (f.readline ()) # once, print a single line 
Print (f.readlines ())     # print all content 
Print (f.read ())
 Print (f.readable ())
 Print (f.read ())
 for i in f:
     Print (i ) 

with Open ( ' b.txt ' , ' W ' , encoding =' GBK ' ) AS f: 
    f.write ( " Shanghai Campus, the first commander -Sean " ) 
    f.writelines ([ " am not overturned \ the n- " , ' I'm glad ' ])   


with Open (r ' dir \ aaaaa. TXT ' , ' a ' , encoding = ' GBK ' ) AS F:
     Print (f.writable ()) 
    f.write ( " \ n-roll is impossible " )

 

 

Guess you like

Origin www.cnblogs.com/ludingchao/p/11815321.html