文件备份问题

文件备份问题

https://img1.sycdn.imooc.com//climg/622c0d0409e2e20c16861179.jpg

https://img1.sycdn.imooc.com//climg/622c0d270985b21718721011.jpg

为什么运行之后只能在文件夹中显示文件备份结果,但是python结果界面中没有显示print中的各种提示语,比如开始备份,备份完成之类的?

应该如何使其显示?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕凡 2022-03-12 11:13:19

同学,你好!

正常是可以显示的,同学可以将代码复制提交一下,老师这边运行一下,更好为同学解决问题,祝学习愉快~

  • 提问者 慕桂英6320947 #1
    os
    os.path
    
    Backup():
        (srcdist):
            .src = src
            .dist = dist
        ():
            ls = os.listdir(.src)
            (ls)
            i ls:
                .back_files(i)
    
        (file_name):
            os.path.exists(.dist):
                os.makedirs(.dist)
                ()
            full_src_path = os.path.join(.srcfile_name)
            full_dist_path = os.path.join(.distfile_name)
            os.path.isfile(full_src_path) os.path.splitext(full_src_path)[-].lower() ==:
                (full_src_path)
            (full_dist_path=) f_dist:
                    (.format(file_name))
                    (full_src_path=) f_src:
                        :
                            rest = f_src.read()
                            rest:
                                f_dist.write(rest)
                        f_dist.flush()
                    (.format(file_name))
            :
                ()
    
    
    
    __name__ == :
        base_path = os.path.dirname(os.path.abspath(__file__))
        src_path = os.path.join(base_path)
        dist_path = os.path.join(base_path)
        bak = Backup(src_pathdist_path)


    2022-03-14 09:49:31
  • 提问者 慕桂英6320947 #2
    import os
    import os.path
    
    class Backup(object):
        def __init__(self,src,dist):
            self.src = src
            self.dist = dist
        def read_files(self):
            """
            读取scr目录下的所有文件
            :param self:
            :return:
            """
            ls = os.listdir(self.src)
            print(ls)
            for i in ls:
                self.back_files(i)
    
        def back_files(self,file_name):
            """
            处理备份
            """
            #1.判断dist是否存在,如果不存在,要创建这个目录
            if not os.path.exists(self.dist):
                os.makedirs(self.dist)
                print("指定的目录不存在,创建完成")
            #2.判断文件是否为我们要备份的文件
            #拼接完整路径
            full_src_path = os.path.join(self.src,file_name)
            full_dist_path = os.path.join(self.dist,file_name)
            #首先判断是否为文件,然后借助于文件的后缀名进行判断
            if os.path.isfile(full_src_path) and os.path.splitext(full_src_path)[-1].lower() ==".txt":
                print(full_src_path)
            #3.读取文件内容
                with open(full_dist_path,"w",encoding="utf-8") as f_dist:
                    print("<<开始备份[{0}]".format(file_name))
                    with open(full_src_path,"r",encoding="utf-8") as f_src:
                        while True:
                            rest = f_src.read(100)
                            if not rest:
                                break
                            f_dist.write(rest)
                        f_dist.flush()
                    print("<<<[{0}]备份完成".format(file_name))
            #4.把读取到的内容写入到新的文件中
            else:
                print("文件类型不符合备份要求,跳过<<")
    
    
    
    if __name__ == "__main__":
        base_path = os.path.dirname(os.path.abspath(__file__))
        #print(base_path)
        src_path = os.path.join(base_path,"src")
        #print(src_path)
        dist_path = os.path.join(base_path,"dist")
        #print(dist_path)
        bak = Backup(src_path,dist_path)


    2022-03-14 09:51:37
  • 好帮手慕美 回复 提问者 慕桂英6320947 #3

    同学,你好!同学需要调用read_files()方法读取目录下的文件

    https://img1.sycdn.imooc.com//climg/622ea6e709c52ccc07670301.jpg

    https://img1.sycdn.imooc.com//climg/622ea75f0958d4a115310209.jpg

    祝学习愉快!

    2022-03-14 10:25:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
1.Python零基础入门
  • 参与学习           人
  • 提交作业       2727    份
  • 解答问题       8160    个

想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师