简体   繁体   English

理解 os.walk Python

[英]Understanding os.walk Python

I'm trying to walk over the directory structure and create a similar structure (but not identical).我正在尝试遍历目录结构并创建一个类似的结构(但不完全相同)。

I got confused of the use of os.path.join, the following code with 2 or more directory depth works perfectly.我对 os.path.join 的使用感到困惑,以下具有 2 个或更多目录深度的代码完美运行。

DIR_1 :目录_1:

A |一个 | file2.txt文件2.txt

B |乙 | file3.txt文件3.txt

file1.txt文件1.txt

inputpath = DIR_1
outputpath = DIR_2
for dirpath, dirnames, filenames in os.walk(inputpath):
    structure = os.path.join(outputpath, dirpath[len(inputpath):])
        for f1 in filenames:
        f = os.path.splitext(f1)[0]
        path = structure + '/' + f
        print ("The path is: ", path)
        file1 = path + '/' + f1
        print ("The file path is: ", file1)
        file_dir = dirpath + '/' + f1;
        print ("The file dir path is: ", file_dir)
        print ("\n")

But in case of just one level of depth, it add additional '/'.但是在只有一层深度的情况下,它会添加额外的“/”。 Is there a way to avoid this?有没有办法避免这种情况?

For example the following gives:例如以下给出:

The path is:  DIR_2//file1
The file path is:  DIR_2//file1/file1.txt
The file dir path is:  DIR_1/file1.txt


The path is:  /A/file2
The file path is:  /A/file2/file2.txt
The file dir path is:  DIR_1/A/file2.txt


The path is:  /B/file3
The file path is:  /B/file3/file3.txt
The file dir path is:  DIR_1/B/file3.txt

Edit 1:编辑1:

The output directory DIR_2 structure is similar to the original Dir_1 but not identical.输出目录 DIR_2 结构类似于原始 Dir_1 但不完全相同。

The DIR_2 should have additional one level of directory of the filename; DIR_2 应该有额外的一级目录文件名; for example rather than just例如,而不仅仅是

DIR_2/file1.txt DIR_2/file1.txt

it should be它应该是

DIR_2/file1/file1.txt. DIR_2/file1/file1.txt。

DIR_2/A/file2/file2.txt. DIR_2/A/file2/file2.txt。 Similarly.相似地。

Edit 2: I also need to read the content of the dirpath (of DIR_1) and select relevant text to put in the corresponding output file (of DIR_2).编辑2:我还需要读取dirpath (DIR_1)的内容并选择相关文本放入相应的输出文件(DIR_2)。 So i can't ignore it.所以我不能忽视它。

You should not worry about the dirpath , use it only to get the original files, all information to recreate the directory structure you already have in dirnames .您不应该担心dirpath ,仅使用它来获取原始文件,所有信息以重新创建您在dirnames已有的目录结构。 The code to recreate the file structure can look like this:重新创建文件结构的代码如下所示:

for root, dirs, files in os.walk( input_path ) :
    offset = len(input_path)
    if len(root) > len(input_path) :
        offset += 1   # remove an extra leading separator

    relative_path = root[offset:]

    for d in dirs :  # create folders
        os.mkdir( os.path.join( output_path, relative_path, d )


    for f in files : # copy the files
        shutil.copy( os.path.join( root, f),
                     os.path.join( output_path, relative_path, f))

And that's it!就是这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM