python - Trying renaming all files in a folder -


i trying script below rename files in folder.it working fine,but when trying run outside folder.it shows error.

import os  path=os.getcwd() path=os.path.join(path,'it') filenames = os.listdir(path) i=0 filename in filenames:     os.rename(filename, "%d.jpg"%i)     i=i+1 

'it' name of folder in files lie. error:filenotfounderror: [errno 2] no such file or directory: '0.jpg' -> '0.jpg' print showing names of files

when os.listdir(path) filenames of files in folder, not complete paths files. when call os.rename need path file rather filename.

you can join filename parent folder's path using os.path.join. e.g. os.path.join(path, file).

something might work:

for filename in filenames:     old = os.path.join(path, filename)     new = os.path.join(path, "%d.jpg"%i)     os.rename(old, new)     i=i+1 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -