python3中的索引文件夹和文件

时间:2018-01-15 11:01:52

标签: python python-3.x indexing

我想创建一个我在企业云中拥有的所有文件和文件夹的索引,这样我就可以对其进行备份并将索引存储到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

有另一个想法,如果我用值创建一个数组怎么办?例如:

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

所以这样我会对主文件夹中的所有值执行for循环,抛出子文件夹的所有值等等...

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

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

2 个答案:

答案 0 :(得分:0)

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

那就是说,如果你想自己做,你可能需要这样的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)

也许您还需要通过将标题作为参数添加到标题中来跟踪标题。

答案 1 :(得分:0)

这是一种可能的解决方案

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()