제1 부분 day 5 파일 조작

원본 링크:http://www.cnblogs.com/rise-home/p/11558095.html
#-----    -----

1、"r" 2、"w" 3、"a" 4、"r+" , 5、"w+" , 6、"a+"

1 with open("   ","r",encoding="utf-8") as f:
2     data = f.read()
3     print(data)
4     f.close()
readlines   
1 with open("   ","r",encoding="utf-8") as f:
2     data = f.readlines()
3     for i in data:
4         print(i.strip())
5 
6 with open("   1","w",encoding="utf-8") as f:
7     f.write("       。     ,   。       。")
#     6   
 1 with open("   ","r",encoding="utf-8") as f:
 2     # print(f.fileno()) #     #3
 3     num1 = 0
 4     for i in f.readlines():
 5         num1 += 1
 6         if num1 == 6:
 7             i = ''.join([i.strip(),'ok'])
 8         print(i.strip())
 9 
10 with open("   ","r",encoding="utf-8") as f:
11     num1 = 0
12     for i in f:  #for     f          ,      
13         num1 += 1
14         if num1 == 4:
15             i = "".join([i.strip(),"ok"])
16         print(i.strip())
#flush   (      )
 1 import time,sys
 2 
 3 for i in range(30):
 4     sys.stdout.write("*")
 5     time.sleep(0.2)
 6     sys.stdout.flush()
 7 
 8 for i in range(30):
 9     print("*",end="",flush=True)
10     time.sleep(0.2)
#truncate   
1 with open("   1","a",encoding="utf-8") as f:
2     f.truncate(6) #  
#r+         0  
1 with open("   1","a+",encoding="utf-8") as f:
2     f.write("  ") #      
3     f.close()
#w+   
1 with open("   1","w+",encoding="utf-8") as f:
2     f.write("  ")
3     print(f.tell()) #6
4     f.seek(0)
5     print(f.readline())
#a+           
1 with open("   1","a+",encoding="utf-8") as f:
2     print(f.tell())
3     f.write("  ")
4     print(f.tell())
5     print(f.readline())
#    
 1 # f_read = open("   1",'r',encoding="utf-8")
 2 # f_write = open("   2",'w',encoding="utf-8")
 3 #with           
 4 with open("   1",'r',encoding="utf-8") as f_read,open("   2",'w',encoding="utf-8") as f_write:
 5     num1 = 0
 6     for line in f_read:
 7         num1 += 1
 8         if num1 == 5:
 9             line = "".join([line.strip(),"dream
"]) 10 f_write.write(line) 11 print(line.strip()) 12 f_write.close() 13 f_read.close()

다음으로 전송:https://www.cnblogs.com/rise-home/p/11558095.html

좋은 웹페이지 즐겨찾기