Vineet Faske
Vineet Faske

Reputation: 1

os.rename( ) is giving the error FileNotFoundError: [WinError 2] The system cannot find the file specified: '0.jpg' -> '2.jpg'

I am trying to rename every image file in a folder to names like 1.jpg , 2.jpg and so on using Python. I have written the following code but it is not working and it gives the following error:

FileNotFoundError: [WinError 2] The system cannot find the file specified: '0.jpg' -> '2.jpg'

Code:

import os
# changing the directory to where the image files are located
os.chdir(r"F:\Images\7 WONDER GARDEN")

for file in os.listdir():
    for num in range(len(os.listdir())):
        os.rename(file, str(num) + ".jpg")

I have tried writing the path in os.rename but still it is giving the same error. Please help me to get rid of this problem. Thank you for taking the effort to read this question.

Upvotes: 0

Views: 1161

Answers (4)

E. Ducateme
E. Ducateme

Reputation: 4248

As noted, the double for loop is what is causing the problem. The inner for loop tries to repeatedly change the name of a file over and over, but once that file's name is changed the first time, it can no longer be found.

The Pythonic solution to this is to use a function called enumerate(). enumerate() returns a sequence of paired items: an index and an item from the sequence you feed into enumerate().

In our case, the list of file names goes into enumerate() and a series of index and filename pairs comes out...

(0, a.jpg)
(1, b.jpg)
(2, c.jpg) 

Applying this to your code... If you use two target variables instead of one in the for loop (in this case num and file, the for loop will automagically unpack the values returned by enumerate, a pair at a time and within the for loop, you can then use the pairs of values to help with file renaming, as shown below.

for num, file in enumerate(os.listdir()):
    os.rename(file, str(num) + ".jpg")

Of interest is the fact that enumerate can be set to output values that start from any initial starting point, using a start argument:

for pair in enumerate(files, 1000):

would end up producing paired values that look like this:

(1000, a.jpg)
(1001, b.jpg)
(1002, c.jpg) 

Upvotes: 1

Lavanya V
Lavanya V

Reputation: 357

try this

num = 0
for file in os.listdir():
    os.rename(file,str(num) + ".jpg")
    num = num+1

Upvotes: 0

Błotosmętek
Błotosmętek

Reputation: 12927

You have double loop here, so you're trying to rename first file to 0.jpg, then to 1.jpgand so no. Of course, after first rename no more renames are possible. What you really wanted is:

for num, file in enumerate(os.listdir()):
    os.rename(file, str(num) + ".jpg")

Upvotes: 1

Cblopez
Cblopez

Reputation: 586

You are renaming one single file len(os.listdir()) times, so the first inner for loop iteration will work, but it won't work anymore once the original file is no longer there. Try the following:

import os
# changing the directory to where the image files are located
os.chdir(r"F:\Images\7 WONDER GARDEN")

for index, file in enumerate(os.listdir()):
    os.rename(file, str(index) + ".jpg")

Upvotes: 2

Related Questions