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.