python basics: reading and writing files


1. Whether reading and go after the end of the function to be used for close, make sure to write or read the file read and write correctly save close
  if not close the only open file will take up computer memory.
The latter may be replaced w 2. Path a, a representative of append function, can increase the original data file 
  w represents the write function, if not a, the original document may be covered
  with a or w, if there is no file path, can be generated automatically file
3. The function used 
  open () function: Open the
  read () function: read
  write () function: write
  close () function: Close
  append () function: increase
# Write to file 
# written at an absolute path name txt file encoding format 1 is utf-8, and assigned to ff = open (r'c: \ Users \ Administrator \ Desktop \ test \ 1.txt ', 'W', encoding = 'UTF-. 8') 
f.write ( ' Year 2020 \ n- ' ) # add content and wrap 
f.write ( ' learn, every day \ n- ' ) 
f.close # close the file f 
# read file 
# open absolute path name txt file output format. 8-a is UTF 
F2 = open (R & lt ' C: \ the Users \ Administrator \ Desktop \ Test \ 1.txt ' , ' R & lt ' , encoding = ' UTF-. 8 ' ) 
A = F2.the Read () # read the file and assigned to the variable f A 
Print(a) # print a content 
f2.close () # close the file f

Guess you like

Origin www.cnblogs.com/wuzhuangzhuang/p/12124885.html