标签:pyc 返回值 覆盖 adl 数据 code 方法 bsp 读文件
1、读文件
import codecs f = codecs.open(‘1.txt‘) text = f.read() result = text.replace(‘today‘, ‘tomorrow‘) print(result) f.close()
2、写文件
import codecs f = codecs.open(‘1.txt‘, ‘w‘) f.write(‘today is sunday‘) f.close() #codecs 用来解决文件乱码的问题 #open(filename, mode)
import codecs file = codecs.open(‘3_2.txt‘, ‘wb‘) print(dir(file)) file.write(‘hello world!\nshenzhen\n‘) print(file.tell()) file.writelines([‘aaaa\n‘, ‘bbbb\n‘, ‘cccc\n‘, ‘dddd\n‘]) print(file.tell()) file.flush() file.seek(0) file.write(‘this is theather is so very cool‘) file.close()
4、with用法
with open(‘1.txt‘) as fd:
    print(fd.read())
    print(fd.closed)
print(fd.closed)
D:\Python27\python.exe D:/PycharmProjects/learn5/5.1.py
today is sunday
False
True
Process finished with exit code 0 
with open(‘3_2.txt‘) as fd:
    for i, line in enumerate(fd):
        print(i, line,)
标签:pyc 返回值 覆盖 adl 数据 code 方法 bsp 读文件
原文地址:http://www.cnblogs.com/yshan13/p/7745844.html