43-练习:生成文本文件
作者:
凯茜的老爸 | 来源:发表于
2018-07-31 08:49 被阅读8次import os
def get_fname():
while True:
fname = input('filename: ')
if not os.path.exists(fname):
break
print('%s already exists. Try again' % fname)
return fname
def get_content():
content = []
print('输入数据,输入end结束')
while True:
line = input('> ')
if line == 'end':
break
content.append(line)
return content
def wfile(fname, content):
with open(fname, 'w') as fobj:
fobj.writelines(content)
if __name__ == '__main__':
fname = get_fname()
content = get_content()
content = ['%s\n' % line for line in content]
wfile(fname, content)
本文标题:43-练习:生成文本文件
本文链接:https://www.haomeiwen.com/subject/gslcvftx.html
网友评论