python--文件读写

# f = open('a.txt', 'w', encoding='utf-8')
# names = ['a', 'b', 'c']
# for name in names:
# f.write(name)
# f.writelines(names)
# f.close()
s = '既然青春留不住'
f.write(s) # 效率高
f.writelines(s) # 效率不高

# import time


# 进行写操作发现没有写进内容,可调用flush()
# f = open('a.txt', 'w')
# f.write('123')
# f.flush() # 立即把缓冲区里面的内容写到磁盘里面
# f.close()
# time.sleep(50)

# 用with可自动关闭文件,可打开多个文件用,分割
# with open('a.txt', 'w') as f, open('b.txt', 'w') as f1:
# f.write('666')
# f1.write('777')

# rb
# wb
# ab
# with open('韩佳人.jpg', 'rb') as f: # 以二进制的模式打开方式
# print(f.read())
原文地址:https://www.cnblogs.com/wangsilei/p/8251232.html