代码:
#!/user/nim/python
#-*-coding: UTF-8 -*-
import os
file=open("a1.txt","w") #创建文件,只用于写入
print "file name:",file.name #文件名
print "is closed:",file.closed #文件是否关闭
f=open("a2.txt","w+") #创建文件,可读写
f.write("hello,world") #向文件写入内容
s=f.read() #读取文件内容
print s
os.rename("a1.txt","a.txt") #文件改名
os.remove("a.txt") #删除文件
os.remove("a2.txt")
运行结果:
file name: a1.txt
is closed: False
hello,world