-1

I have a spimple script to rename all files in directory (ex. 475435_name.psd) by increasing its number lets say by 10.

But I get an error

Traceback (most recent call last):
  File "C:/Users/mstopienski/Desktop/Desktop/test.py", line 12, in <module>
    os.rename(filename, newname)
FileNotFoundError: [WinError 2] The system cannot find the file specified: '810858_Hero_ProjectHP_1600x487.psd' -> '810868_Hero_ProjectHP_1600x487.psd'

I dont want it to move files I just want the names changed.

import glob, os
path = input()
for filename in glob.glob(path):
    number = filename[0:6]
    name = filename[6:]
    x = int(number)+10
    newname = (str(x) + name)
    os.rename(filename, newname)
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
MaciejPL
  • 1,017
  • 2
  • 9
  • 16
  • 3
    It tries to find the file in the current directory. This is probably not the same as where the file is located. So either provide full path or change current directory. – RvdK Jan 09 '15 at 14:29
  • That being the case, probably changing the last line to: os.rename(os.path.join(path, filename), os.path.join(path, newname)) would do it? – LexyStardust Jan 09 '15 at 14:44
  • The problem is that there is no second file. '810868_Hero_ProjectHP_1600x487.psd' is created of the first one It looks like he is lookin for this second file to overwrite? – MaciejPL Jan 09 '15 at 14:46

2 Answers2

0

As per the comment from @RvrK, the script is trying to target the working directory for the rename so I'm guessing chaning the last line to:

os.rename(os.path.join(path, filename), os.path.join(path, newname)) 

Should do it?

Edit:

Adding some 'safety' features:

import glob, os
path = input()
for filename in glob.glob(path):
    basename = os.path.basename(filename)
    number = basename [0:6]
    name = basename [6:]
    x = int(number)+10
    newname = (str(x) + name)
    os.rename(os.path.join(path, filename), os.path.join(path, newname)) 
LexyStardust
  • 1,018
  • 5
  • 18
0

You need over look the path you input and see if a file ends with '.psd', try this snippet code will help:

import os
path = raw_input()
for filename in os.walk(path):
  for ele in filename:
    if type(ele) == type([]) and len(ele)!=0:
      for every_file in ele:
        if every_file.endswith('psd'):

           number = every_file[0:6]
           name = every_file[6:]
           x = int(number)+10
           newname = (str(x) + name)
           os.rename(os.path.join(path, every_file), os.path.join(path, newname))

I tied put the script and your psd files in the same path input "./" it works.

lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30