现在的位置: 首页 > 综合 > 正文

创建文件(makeTexFile.py)

2018年12月26日 ⁄ 综合 ⁄ 共 475字 ⁄ 字号 评论关闭

创建一个尚不存在的文件名,然后由用户输入数据写入到文本文件。

#makeTextFile.py -- create text file

import os

#get filename

while True:

          fname = raw_input('Enter file name:')

          if os.path.exists(fname):

                 print "ERROE: '%s' already exists" % fname

          else:

                 break

#get file content (text) lines

all = []

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

#loop until user terminates input

while True:

         entry = raw_input('> ')

         if entry == '.':

                 break

         else:

                all.append(entry)

# write lines to file with proper line-ending

fobj = open(fname, 'w')

fobj.write('\n'.join(all))

fobj.close()

print 'DONE!'

抱歉!评论已关闭.