python basic file operations

Refreshed in real time to the hard disk

Open = F ( 'HH', 'W', encoding = 'UTF8') 
f.write ( 'gyftyftft') 
f.write ( 'hghgh \ njkkjk') 
f.flush () in real time to the disks #

 Print Download entry

import sys, time # load modules 
for I in Range (30): 
    sys.stdout.write ( '*') Print # * 
    sys.stdout.flush () # real brush disk 
    time.sleep (0.2) # 0.2 seconds delay

  test

D:\python\python.exe D:/untitled/dir/for.py
******************************
Process finished with exit code 0

  original

nihao chenxi haha woai ni 
do not see tears when Man for years. Xichuang wax torch still Lan. Overburdened between dream ten years. 
Shank and down days straight north, take off the melancholy age of the Lanna force. No one more solution Yi Chang.

  Before leaving only five characters in the file

o = open('尘曦','a',encoding='utf8')

o.truncate(5)
o.close()

  After running the code view

nihao

  r + mode the file operation modes

o = open ( 'dust Xi', 'R & lt +', encoding = 'UTF8') 
print (o.read ()) 
o.close () 
o.write ( 'Fei') # note added to the final 
print (o.read ()) 
o.close ()

  test

D: \ Python \ python.exe D: /untitled/dir/for.py 
NiHao Chenxi haha WOAI Ni 

do not see tears when Man for years. Xichuang wax torch still Lan. Overburdened between dream ten years. 
Shank and down days straight north, take off the melancholy age of the Lanna force. No one more solution Yi Chang. Yue Fei 

Process finished with exit code 0

  The file operation w +

o = open('尘曦','w+',encoding='utf8')
print(o.readline())
o.write('岳飞')
#print(o.tell())
o.seek(0)
print(o.read())
o.close()

  test 

D:\python\python.exe D:/untitled/dir/for.py

岳飞

Process finished with exit code 0

     original

nihao chenxi haha woai ni 

do not see tears when Man for years. Xichuang wax torch still Lan. Overburdened between dream ten years. 
Shank and down days straight north, take off the melancholy age of the Lanna force. No one more solution Yi Chang.  

The file operation a +

o = open('尘曦','a+',encoding='utf8')
print(o.readline())
o.write('岳飞')
o.seek(0)
print(o.read())
o.close()

  test

nihao chenxi haha woai ni 

do not see tears when Man for years. Xichuang wax torch still Lan. Overburdened between dream ten years. 
Shank and down days straight north, take off the melancholy age of the Lanna force. No one more solution Yi Chang. Yue Fei

     original

Health as odds, death is also a male ghost. 
Still thinking Xiang Yu refused to have Koto. 
Do not see tears when Man for years. Xichuang wax torch still Lan. Overburdened between dream ten years. 
Shank and down days straight north, take off the melancholy age of the Lanna force. No one more solution Yi Chang.

 File modification operations

f_red = open('尘曦','r',encoding='utf8')
f_write = open('尘曦-3','w',encoding='utf8')
number = 0
for line in f_red:
    number+=1
    if number==2:
        #line=''.join([line.strip(),'chenxi'])
        line='hello chenxi\n'
    f_write.write(line)

f_red.close()
f_write.close()

  test

Health as odds, death is also a male ghost. 
hello chenxi 
do not see tears when Man for years. Xichuang wax torch still Lan. Overburdened between dream ten years. 
Shank and down days straight north, take off the melancholy age of the Lanna force. No one more solution Yi Chang.

  

  

Guess you like

Origin www.cnblogs.com/rdchenxi/p/11122738.html
Recommended