【问题标题】:Index folders and files in python3python3中的索引文件夹和文件
【发布时间】:2018-01-15 11:01:51
【问题描述】:

我想为我在企业云中拥有的所有文件和文件夹创建一个索引,这样我就可以对其进行备份并将索引存储到一个 txt 文件中,然后我将比较最后的两个备份( txt 文件)来检查哪些文件/文件夹更改了名称。

我已经尝试过这段代码,但它仅限于我插入到代码中的 for 循环的时间,我想知道我是否可以以几乎无限的方式做到这一点,因为我不知道是否有人会添加文件夹到任何子文件夹:

    for f in next(os.walk(path))[1]:
    folder=('{}' + ". " + f).format(a)
    print(folder,file=open(filename, "a"))
    path1=path+f
    for f in next(os.walk(path1))[2]:
        files = ("   " + f)
        print(files,file=open(filename, "a"))
    b=1
    for f in next(os.walk(path1))[1]:
        folder=(" " + '{}' + "." + '{}' + ". " + f).format(a,b)
        print(folder,file=open(filename, "a"))
        path2=path1+("\\")+f
        for f in next(os.walk(path2))[2]:
            files = ("   " + f)
            print(files,file=open(filename, "a"))
        c=1
        for f in next(os.walk(path2))[1]:
            folder=("    " + '{}' + "." + '{}' + "." + '{}' + ". " + f).format(a,b,c)
            print(folder,file=open(filename, "a"))
            path3=path2+("\\")+f
            for f in next(os.walk(path3))[2]:
                files = ("      " + f)
                print(files,file=open(filename, "a"))
            d=1
            for f in next(os.walk(path3))[1]:
                folder=("       " + '{}' + "." + '{}' + "." + '{}' + "." + '{}' + ". "  + f).format(a,b,c,d)
                print(folder,file=open(filename, "a"))
                path4=path3+("\\")+f
                for f in next(os.walk(path4))[2]:
                    files = ("         " + f)
                    print(files,file=open(filename, "a"))
                e=1
                for f in next(os.walk(path4))[1]:
                    folder=("          " + '{}' + "." + '{}' + "." + '{}' + "." + '{}' + "." + '{}' + ". "  + f).format(a,b,c,d,e)
                    print(folder,file=open(filename, "a"))
                    path5=path4+("\\")+f
                    for f in next(os.walk(path5))[2]:
                        files = ("            " + f)
                        print(files,file=open(filename, "a"))
                    g=1
                    for f in next(os.walk(path5))[1]:
                        folder=("             " + '{}' + "." + '{}' + "." + '{}' + "." + '{}' + "." + '{}' +  "." + '{}' + ". "  + f).format(a,b,c,d,e,g)
                        print(folder,file=open(filename, "a"))
                        path6=path5+("\\")+f
                        for f in next(os.walk(path6))[2]:
                            files = ("               " + f)
                            print(files,file=open(filename, "a"))
                        h=1
                        for f in next(os.walk(path6))[1]:
                            folder=("                " + '{}' + "." + '{}' + "." + '{}' + "." + '{}' + "." + '{}' +  "." + '{}' +  "." + '{}' + ". "  + f).format(a,b,c,d,e,g,h)
                            print(folder,file=open(filename, "a"))
                            path7=path6+("\\")+f
                            for f in next(os.walk(path7))[2]:
                                files = ("                  " + f)
                                print(files,file=open(filename, "a"))
                            i=1
                            for f in next(os.walk(path7))[1]:
                                folder=("                   " + '{}' + "." + '{}' + "." + '{}' + "." + '{}' + "." + '{}' +  "." + '{}' +  "." + '{}' +  "." + '{}' + ". "  + f).format(a,b,c,d,e,g,h,i)
                                print(folder,file=open(filename, "a"))
                                path8=path7+("\\")+f
                                for f in next(os.walk(path8))[2]:
                                    files = ("                     " + f)
                                    print(files,file=open(filename, "a"))
                                for f in next(os.walk(path8))[1]:
                                    folder=("                      " + '{}' + "." + '{}' + "." + '{}' + "." + '{}' + "." + '{}' +  "." + '{}' +  "." + '{}' +  "." + '{}' +  "." + '{}' + ". "  + f).format(a,b,c,d,e,g,h,i,j)
                                    print(folder,file=open(filename, "a"))
                                    path9=path8+("\\")+f
                                    for f in next(os.walk(path9))[2]:
                                        files = ("                        " + f)
                                        print(files,file=open(filename, "a"))
                                    k=1
                                    for f in next(os.walk(path9))[1]:
                                        folder=("                         " + '{}' + "." + '{}' + "." + '{}' + "." + '{}' + "." + '{}' +  "." + '{}' +  "." + '{}' +  "." + '{}' +  "." + '{}' +  "." + '{}' + ". "  + f).format(a,b,c,d,e,g,h,i,j,k)
                                        print(folder,file=open(filename, "a"))
                                        path10=path9+("\\")+f
                                        for f in next(os.walk(path10))[2]:
                                            files = ("                           " + f)
                                            print(files,file=open(filename, "a"))
                                        k=k+1
                                    j=j+1
                                i=i+1
                            h=h+1
                        g=g+1
                    e=e+1
                d=d+1
            c=c+1
        b=b+1
    a=a+1

有没有可能做我想做的事?

在代码的开头,我要询问云的主文件夹在哪里(在 windows 上),因为我们都使用 OneDrive 应用程序将云放在计算机中。

这是一个关于我希望如何显示的示例:

2. Handover
 2.1. 2018
    2.1.1. 1_January
      RE  0112018 Report A (by PC).msg
      RE  0112018 Report M (by FM).msg
      RE  0112018 Report N (by RB).msg

有了另一个想法,如果我用这些值创建一个数组会怎样。例如:

[主文件夹,子文件夹,子子文件夹,...]

因此,我将对主文件夹上的所有值执行 for 循环,以抛出子文件夹的所有值等...

然后关联“1”。到数组的第一个值“1.1”。到第二个,等等

这是一件简单的事情吗?并检查它是否有一个子文件夹,是否必须添加一个数组值?

【问题讨论】:

    标签: python python-3.x indexing


    【解决方案1】:

    几乎可以肯定,已经有一个工具可以像您想要的那样索引文件。

    也就是说,如果你想自己做,你可能需要这样的recursive function(伪代码):

    def get_files(path, indent):
        for f in children_in_path:
            # print appropriately, e.g. print('{} {}'.format(indent*' ', f)
            next_path = path+f
            get_files(next_path, indent+2)
    

    也许您还需要通过将标题作为参数包含在您的函数中来跟踪它们。

    【讨论】:

    • 我应该在缩进中输入什么?因为它说缩进没有定义,如果我插入一个数字,它说它应该是一个 str 而不是一个 int
    【解决方案2】:

    这是一种可能的解决方案

    from pathlib import Path
    
    
    def iter_subtree(path, layer=0):
        """Recursively iterate over each item in path 
           and print item's name.
        """
    
        # make Path object from input string
        path = Path(path)
    
        # iter the directory
        for p in path.iterdir():
    
            if p.is_file():
                print('\t'*layer, p.name)
    
            elif p.is_dir():
                print('\t'*layer, p.name+'/')
                iter_subtree(p, layer+1)
    
            else:
                raise FileNotFoundError()
    

    【讨论】:

      猜你喜欢
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多