首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 文本的读写功能整合

python 文本的读写功能整合

作者头像
py3study
发布2020-01-14 11:41:57
3560
发布2020-01-14 11:41:57
举报
文章被收录于专栏:python3python3python3

功能整合

程序如下:

print("*creating and reading text file in on programe*")
while True:
    print("\n=========Meau======")
    print("a. create text file")
    print("b. read text file")
    print("q. quit")
    print(" "*11,"All by Neo")
    print('- '*10)
    choice = input("\nWhat do U want ?:")

    if  choice == 'a':
        print("\n\n***create text file***")
        import os
        ls = os.linesep
        #get filename
        while True:
            fname = input("Enter your file name:")
            if os.path.exists(fname):
                judge = input("File already exists,try to overwrite it(y/n)?:")
                if judge == 'y':
                    os.remove(fname) #if 'y' ,remove old file
                    break
                if judge == 'n':
                    break
            else:
                break

        #get file content lines
        all = []
        print("\nEnter your lines('quit'by itself to quit).")
        while True:
            entry= input('>')
            if entry == 'quit':
                break
            else:
                all.append(entry)
        #write lines to file with proper line-ending
        fobj = open(fname, 'w')
        fobj.writelines(['%s%s' %(x,ls) for x in all])
        fobj.close()
        print('Done')

    if choice == 'b':
        #read and display text file
        print("\nread and display text file")
        fname = input("\n\nEnter the filename:")
        print
        #attemp to open file for reading
        try:
            fobj = open(fname,'r')
        except IOError:
            print("file open error:")
        else:
            #display contents
            print('- '*5,"contents below",'- '*5)
            for eachline in fobj:
                print(eachline,end = '')
            fobj.close()
        print('- '*7,"Done",'- '*7)
    if choice == 'q':
        break
print ("\nDone,bye")
 
程序功能有限

对文本的编辑功能尚在学习中

try more

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档