返回字符串
file = input() #返回字符串 fo = open(file,"r").read(6) print(fo)
返回列表形式
file = input() fo = open(file,"r") #print(fo) #返回列表形式 print(fo.readlines(-1))
file = input() fo = open(file,"r") for line in fo.readlines(): print(line.replace('\n',"")) fo.close()
file = input() fo = open(file,"r") for line in fo: print(line.replace('\n',"")) fo.close()
w+
打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
fname = input() fo = open(fname,"w+") ls = ["TreeDream","love","sea"] fo.writelines(",".join(ls)) fo.seek(0) for line in fo: print(line) fo.close()