python 创建新文件

来源:互联网 发布:大数据架构师面试题 编辑:程序博客网 时间:2024/06/13 02:12
这个脚本提醒用户输入一个(尚不存在的)文件名, 然后由用户输入该文件的每一行后, 将所有文本写入文本文件。 
    '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!'

运行界面:

这里写图片描述

这里写图片描述

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

0 0
原创粉丝点击