1

I have a list of files. Each filename contains a digit which I want to remove. I then want to rename the file with the new filename (without digits). Getting an error below:

Code below :

import os, re
mypath = "/Users/tikka/Downloads/prank/prank"
for f in os.listdir(mypath):
 print f
 frenamed = re.sub('\d','',f)
 print frenamed
 os.rename(f,frenamed)
 print f

Error Message

Traceback (most recent call last):
  File "C:/Python27/ty3.py", line 7, in <module>
    os.rename(f,frenamed)
WindowsError: [Error 2] The system cannot find the file specified

This question is different to Rename Files in Python as my .py is not in the same directory as the files that I wish to rename

Community
  • 1
  • 1
Tikkaty
  • 772
  • 1
  • 8
  • 24

1 Answers1

5

You need to prepend the files with the path you found them in. E.g. something like:

os.rename(os.path.join(mypath, f), os.path.join(mypath, frenamed))
Turn
  • 6,656
  • 32
  • 41