文件新建方法
qpython吧
全部回复
仅看楼主
level 5
风化雪域9 楼主
#-*-coding:utf8;-*-
#qpy:3
#qpy:console
#_file_="hzh.txt"
print('文件新建方法,作者:何在华')
import os
#print(os.chdir(os.path.dirname(__file__)))
os.chdir(os.path.dirname(__file__))#进入文件夹
#print(_file_)
afile=os.getcwd()#绝对路经
afile=afile+"/hzh.txt"#查询绝对路径,加入文件名
#print(afile)
#print(os.path.exists(afile))
if os.path.exists(afile): #判断文件存在否
a1=open(afile,"a") #存在以加的方式写入内容的 打开文件
else:
a1=open(afile,"w") #不存在以写的方式打开新建文件
xrnr0=input("请输入您想写入的内容:")
xh0=0
while xh0<1 :
xh0=xh0+1
jxxr0=input("继续写入吗?")
if (jxxr0=='y' or jxxr0=='Y'):
xrnr0=xrnr0+ input("请输入您想写入的内容:")
print(xrnr0)
xh0=0
a1.write(xrnr0)#写入内容
a1.close() #关闭文件
print("谢谢使用!")
2016年06月10日 04点06分 1
1