Pushpa
Pushpa

Reputation: 458

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.

DIR_1 :

A | file2.txt

B | file3.txt

file1.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:

The output directory DIR_2 structure is similar to the original Dir_1 but not identical.

The DIR_2 should have additional one level of directory of the filename; for example rather than just

DIR_2/file1.txt

it should be

DIR_2/file1/file1.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). So i can't ignore it.

Upvotes: 0

Views: 354

Answers (1)

lenik
lenik

Reputation: 23556

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. 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!

Upvotes: 1

Related Questions