读取文件:
#直接读取
for line in open("d:\serverlist.txt"):
print(line)
#使用readline()方法读取
file = open(‘d:\serverlist.txt‘)
line = file.readline()
while line:
print(line,end=‘’)
file.close()
#使用read()方法读取
f = open("d:\out.txt",‘r‘)
mm=f.read()
f.close()
print(mm)
写入文件:
#新建或覆盖原文件写入
f = open("d:\out.txt", ‘w‘)
print("abc",file=f)
f.close()
#追加写入
f = open("d:\out.txt", ‘a‘)
print("abcdef",file=f)
f.close()
#使用write()方法写入
f = open("d:\out.txt",‘a‘)
f.write("\nnnnn")
f.close()
原文地址:http://www.cnblogs.com/dreamer-fish/p/3818734.html