新建文件并写入内容

import os

def get_fname():
    while True:
        f_name = input('输入用户名:')
        if not os.path.exists(f_name):
            break
        print('%s已存在,青重新输入'%f_name)
    return f_name

def get_content():
    content = []
    while True:
        data = input('input> ')
        if data == 'quit':
            break
        content.append(data)
    return content


def write_file(f_name, f_content):
    with open(f_name, 'w') as f:
        for line in f_content:
            f.write(line+'\n')


def main():
    f_name = get_fname()
    f_content = get_content()
    write_file(f_name, f_content)


if __name__ == "__main__":
    main()

 

posted @ 2019-03-14 11:50  Ray_chen  阅读(343)  评论(0编辑  收藏  举报