Search code examples
pythonrenamebatch-rename

Removing characters and renaming files


Having trouble shortening to the first 13 characters of a file name, any suggestions?

import os

path = '/home/ben/Desktop/UK_DDV'
files = os.listdir(path)

for file in files:
    outfile = file[:13]
    os.rename(os.path.join(path, outfile), os.path.join(path, outfile)+'.tif')

Keeping getting the error

'FileNotFoundError: [Errno 2] No such file or directory:'


Solution

  • The first parameter to os.rename() should be using the original filename, file instead of outfile.