首页 > 编程 > Python > 正文

python 创建新文件

2019-11-06 09:02:04
字体:
来源:转载
供稿:网友
这个脚本提醒用户输入一个(尚不存在的)文件名, 然后由用户输入该文件的每一行后, 将所有文本写入文本文件。 'make TextFile.py--create text file'import osls = os.linesep#get filenamefname = raw_input('enter the filename:')while True: if os.path.exists(fname): PRint"Error:'%s'already exists"% fname else: break#get file content (text) linesall=[]print"/nEnter lines ('.' by itself to quite )./n"#loop until user terminates inputwhile True: entry = raw_input("inputing:") if entry =='.': break else: all.append(entry)#write lines to file with proper line-endingfobj = open(fname,'w')fobj.writelines(['%s%s'%(x,ls)for x in all])fobj.close()print 'DONE!'

运行界面:

这里写图片描述

这里写图片描述

因为时间有限代码解释就没有写的很详细。有问题可以留言。。。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表