Chinaunix首页 | 论坛 | 博客
  • 博客访问: 361694
  • 博文数量: 150
  • 博客积分: 3423
  • 博客等级: 中校
  • 技术积分: 1005
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-15 09:04
文章分类

全部博文(150)

文章存档

2012年(2)

2011年(148)

分类: Python/Ruby

2011-07-30 09:10:04

  1. #!/usr/bin/env python

  2. 'makeTextFile.py -- create text file'

  3. import os
  4. ls=os.linesep

  5. #get filename

  6. while True:
  7.     fname=raw_input('Please input the filename:') #让用户输入一文件名
  8.     if os.path.exists(fname): #判断此文件名是否已经存在
  9.         print "Error :'%s' already exists" % fname
  10.     else:
  11.         break

  12. #get file content (text) lines

  13. all=[] #内容列表

  14. print "\nEnter lines ('.' by itself to quit). \n"

  15. #loop until user terminates input

  16. while True:
  17.     entry=raw_input('>')
  18.     if entry=='.': #用户输入‘.’号时输入终止
  19.         break
  20.     else:
  21.         all.append(entry) #列表数据累加
  22. #write lines to file with proper line-ending

  23. fobj=open(fname,'w') #打开文件
  24. fobj.writelines(['%s%s' % (x,ls) for x in all ]) #将列表内容输入到文件中
  25. fobj.close() #关闭文件
  26. print 'DONE!'
阅读(2536) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~