python_file_learning

# author: Roy.G
import wsgiref.handlers
'''
data=open("file_1.txt",encoding="utf-8")
print(data)
print(data.read())
print("----------------")
print(data.read()) # cursor is in the downmost position, so can't read anything.
data_2=open("file_2.txt","w",encoding="utf-8")
data_2.write("大漠孤烟直,\n")
data_2.write("长河落日圆")
print("---------------")
data_2=open("file_2.txt","a",encoding="utf-8")# a= append
for i in range(10):
data_2.write("大漠孤烟直,\n")
data_2.write("长河落日圆\n")
data_2=open("file_2.txt","r",encoding="utf-8")# a= append
for i in range(13):
print(data_2.readline().strip())

data_3=open("data_3.txt","a",encoding="utf-8")
for i in range(20):
data_3.write(str(i))
data_3.write("\n")
data_3=open("data_3.txt","r",encoding="utf-8")
print(data_3)
for index,line in enumerate(data_3.readlines()):
if index==3:
print("-----------")
continue
print(line.strip())

for line in data_3: # use just one memory space ,increase the efficiency
if count==3:
print("---")
count+=1
print(line)
data_4 = open("data_3.txt", "r", encoding="utf-8")
print(data_4.tell()) # tell the positiong of cursor
print(data_4.read(6))
print(data_4.tell())
data_4.seek(0) #move the cursor to position()
print(data_4.tell())
print(data_4.read(1))
print(data_4.tell())
print(data_4.fileno()) #display the file i/o port
print(data_3.fileno())
print(data_4.seekable())
data_5 = open("data_3.txt", "w", encoding="utf-8")
print(data_5.write("roy"))
print(data_5.flush()) # save input to hard disc

import sys,time
for i in range(20): # progress bar
sys.stdout.write("-")
sys.stdout.flush()
time.sleep(0.1)
data_5 = open("data_3.txt", "w", encoding="utf-8")
data_5.truncate()
'''

# author: Roy.G

import sys
file_1=open("file_1.txt","w",encoding="utf-8")
for i in range(10):
file_1.writelines(str(i))
file_1.writelines("\n")
file_1.flush()
file_2 = open("file_2.txt", "w", encoding="utf-8")
for i in range(10):
file_2.writelines(str(i))
file_2.writelines("\n")
file_2.flush()
with open("file_1.txt","r",encoding="utf-8") as f_1,\
open("file_2.txt", "r", encoding="utf-8") as f_2:
for i in f_1: # close the file ,beyond the with
print(i.strip())


posted on 2021-12-29 12:37  ttm6489  阅读(24)  评论(0编辑  收藏  举报

导航