Python:文件操作(2019-1-15)

2019-01-26  本文已影响0人  MMatx

open("文件名",操作方式)

import os
"""
# 2
f = open('abc.txt','r')
try:
    date=f.read()
    print(data)
finally:
    f.close()

# 3 with as
with open("abc.txt",'r') as f: #可不写r 默认为r
    data=f.read()
    print(data)
"""


f = open('abc.txt','w')
f.write("hello\n")
f.write("MM")

f.close()
f=open('abc.txt','r')
str=f.read()
print(str)
f.close()

os.rename("abc.txt","aa.txt")
上一篇 下一篇

猜你喜欢

热点阅读