2

I have just broken away from LPTHW and I'm trying to get my hands dirty with a renaming script.

I am having problems trying to list the files within a directory, the code is:

def rename_files():
    for root, dirs, files in os.walk(path):
        dirs_len = len(dirs)
        for i in range(0, dirs_len):
            print dirs[i]

The above code will correctly display a list of directories as expected.. However, doing something like this:

    for root, dirs, files in os.walk(path):
        dirs_len = len(dirs)
        for i in range(0, dirs_len):
            print dirs[i]
            print files

will result in an empty list under each directory.

I experimented by creating a files_len and placing it within the nested for-loop:

    for root, dirs, files in os.walk(path):
        dirs_len = len(dirs)
        files_len = len(files)
        for i in range(0, dirs_len):
            print dirs[i]
            print files_len

it results in 0.

If I place the files_len within the first for-loop, it results in 3, which is the correct number of files within a directory.

I wanted it to print something like:

My Pictures

  • img1.png
  • img2.png

And I just cannot figure out how to structure my code to make it behave as I want it to.

Can you point me in the right direction, please?

Many thanks and much appreciated!

-EDIT- The ultimate goal here is to look in each directory, take in a raw_input to change the name of a file, once the last file is reached, move on to the next directory, rinse and repeat.

For example:

Dir1

-> Rename img1.png: img_1.png (<-- raw_input)

-> Rename img2.png: img_2.png (<-- raw_input)

-> Rename img3.png: img_3.png (<-- raw_input)

Dir2

-> Rename img1.png: img_1.png (<-- raw_input)

-> Rename img2.png: img_2.png (<-- raw_input)

-> Rename img3.png: img_3.png (<-- raw_input)

And so on until the last file in the last directory is reached.

3 Answers 3

2

You may try something like this

for root, dirs, files in os.walk(path):
    print("%s:" % root)
    for f in files:
        print ("-> %s" % f)

This will output

My Pictures:

->img1.png

->img2.png

SomeOtherFolder:

->someotherfile.txt

...

Sign up to request clarification or add additional context in comments.

2 Comments

The only problem with this is that it outputs the absolute path. I'm trying to get just the directory name, and then loop through with something like "Rename img1.png: img_1.png" (loop again) "Rename img2.png: img_2.png" (loop again) "Rename img3.png: img_3.png" and so on. it takes a raw_input to ask what to rename each file to before moving on to the next directory.
@DaveMelia - Just use os.path.split -> print (os.path.split("/home/local/user/fun")[1])
0

This code works good for me:

import os

path = "D:\\myfolder"

for root, dirs, files in os.walk(path):
    print "root:", root
    for file in files:
        print "-", file

Comments

0

The os.walk function goes through all of the folders starting from the top. You won't have to manually iterate the 'dirs', that it returns, as it's just a list of the dirs in the current 'root' folder, while 'files' is a list of files in the current 'root' folder.
Therefore, this should do what you want:

for root, dirs, files in os.walk( path ):
    print( "Root: %s\nTotal Files:%i" % ( root, len( files )))
    for file in files:
        print( "\t%s" % file )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.