标签:文件操作 adl width close data 内容 nes writable ima
f = open(‘爱了‘, encoding = ‘utf8‘) data = f.read() print(data) f.close()
f = open(‘爱了‘, ‘r‘, encoding = ‘utf8‘) print(f.readable()) #判断文件是否可读 print(‘第一行‘,f.readline(), end = ‘‘) #一次读一行 print(‘第二行‘,f.readline(), end = ‘‘) #加end是为了取消默认的回车换行 print(‘第三行‘,f.readline()) f.close()
f = open(‘爱了‘, ‘r‘, encoding = ‘utf8‘) print(f.readlines())
f = open(‘金灵‘, ‘w‘, encoding = ‘utf8‘) f.write(‘同学吗\n‘) #自己写换行符,就不会和下面的内容写在一行了 f.write(‘啊啊\n‘) print(f.writable()) #判断是否可写 f.writelines([‘行也思君\n‘, ‘坐也思君\n‘])#以列表形式写入 f.close()
f = open(‘金灵‘, ‘a‘, encoding = ‘utf8‘) f.write(‘写在文件最后\n‘) f.close()
f = open(‘爱了‘, ‘r+‘, encoding = ‘utf - 8‘) data = f.read() print(data) f.write(‘么么\n‘) f.close()
src_f = open(‘金灵‘, ‘r‘, encoding = ‘utf-8‘) data = src_f.readlines() #得到一个列表 src_f.close() dst_f = open(‘金灵_new‘, ‘w‘, encoding = ‘utf-8‘) dst_f.writelines(data[0:4]) dst_f.close()
with open(‘金灵_new‘, ‘r+‘, encoding = ‘utf-8‘) as f: data = f.read() print(data)
标签:文件操作 adl width close data 内容 nes writable ima
原文地址:https://www.cnblogs.com/dabai123/p/11146837.html