28 Minutes Ago | Add Reputation Comment | Flag Bad Post
One last problem with my script, it returns this error message...

Traceback (most recent call last):  File "C:/Python26/Renamer 3", line 23, in <module>    os.rename(fname, b)WindowsError: [Error 2] The system cannot find the file specified.Traceback (most recent call last):
  File "C:/Python26/Renamer 3", line 23, in <module>
    os.rename(fname, b)
WindowsError: [Error 2] The system cannot find the file specified.

This is my almost completed script, feel free to use the completed version.

#!/usr/local/bin/python

import re, os

from os.path import join as pjoin, isdir
while True:
    targetfolder = raw_input('enter a target folder:\n').strip()
    if isdir(targetfolder):
        break
    else:        print("you must enter an existing folder!")
newname = raw_input('enter a new base name:\n').strip()
newname = pjoin(targetfolder, newname)
rxin = raw_input('enter a regex to search for:\n').strip()
allowed_name = re.compile(rxin).match

a = 0

for fname in os.listdir(targetfolder):
    if allowed_name(fname):
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        os.rename(fname, b)
        print((fname, b))

The Print function perfectly shows the name change is possible when the os.rename line is hashed as a comment, and when i alter the variable fname in the line...

os.rename(fname, b)

The line is making the scripts specifically search for files named "fname".

I get messages such as " the rename command only takes 2 variable... you have 3 ect, and "commands or built in fuctions cannot be used, variable one must be a string... when i attempt to alter fname to other possible variables such as rxin, ragex or oldname.

ive tried for about an hour and a half to get this last line right, but i just cant do it.

ive even tried...

os.rename(All, b)
os.rename(oldname, b) 
os.rename((oldfname), b)
os.rename(allowed_name, b)

Recommended Answers

All 10 Replies

I might be on to something for you. First I took out the print line that you have at line 24. Then I put in this statement at line 23, right before the rename happens.

temp = raw_input('about to rename '+fname+' to '+ b)

This shows me what the script is planning to do right before it gets done. What I saw was that the first file (fname) is just a file name, but the second file (b) lists a whole path. If you're running this script from a folder other than the one where you're files are being renamed, then it is very likely that fname will not be found because it is looking for it in the wrong directory.

To test this theory, I moved your script into the same folder with the files that I want to rename and it worked perfectly.

28 Minutes Ago | Add Reputation Comment | Flag Bad Post

Hah, that's awesome...

One last problem with my script, it returns this error message...

Traceback (most recent call last):  File "C:/Python26/Renamer 3", line 23, in <module>    os.rename(fname, b)WindowsError: [Error 2] The system cannot find the file specified.Traceback (most recent call last):
  File "C:/Python26/Renamer 3", line 23, in <module>
    os.rename(fname, b)
WindowsError: [Error 2] The system cannot find the file specified.

This indicates that the "from" file is not located at C:/Python26/Renamer 3... Is that correct? C:/Python26 is likely the current working directory (it's where python.exe is located). And since you just gave the filename as Renamer 3, it assumed this file was in that directory.

To avoid this you can use an absolute path (ie, C:/Foo/Bar/Choo/Renamer 3 or wherever the file is located)

makes sense. I need this program to work from the program directory though, and not have to move it into each folder and set it off like a grenade...

I tried

os.rename(fname in targetfolder, b)

 os.rename(<folder path>fname, b)

 os.rename(<folder path>, b)

But nothing is working, how to i tell the program that i want to rename the files within the folder i told it to look in erlier?

I thought this line would have accomplished that

for fname in os.listdir(targetfolder):

To be honest when this line is run

print((fname, b))

it shows both fname, and b, exactly how i want them, I.E it shows the names of the files i want to rename, and b shows the thenamed version. for example

'FerrariCar.jpg' 'Car1.jpg'
LambourghiniCar.jpg' 'Car2.jpg'
'FordFiestaCar.jpg' 'Car3.jpg"

I just need a command that effectively renames fname(on the left) to b(on the right).

os.rename seems to look in the wrong directory, so will i need another command or do i need to append the os.rename line?

i know the program works up untill the os.rename line, because the "print" line shows me what i want as in the example above

I modified the program slightly for testing, but this work just fine ...

#!/usr/local/bin/python

import re, os
from os.path import join as pjoin, isdir

while True:
    #targetfolder = raw_input('enter a target folder:\n').strip()
    targetfolder = "D:/Renamer3"  # for testing only
    if isdir(targetfolder):
        break
    else:        
        print("you must enter an existing folder!")

newname = raw_input('enter a new base name:\n').strip()
newname = pjoin(targetfolder, newname)

rxin = raw_input('enter a regex to search for:\n').strip()
allowed_name = re.compile(rxin).match

a = 0

for fname in os.listdir(targetfolder):
    print fname, os.getcwd()  # test
    if allowed_name(fname):
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        os.rename(fname, b)
        print((fname, b))

"""my output (Windows XP) -->
enter a new base name:
car
enter a regex to search for:
Pic
old D:\Renamer3
pic.jpg D:\Renamer3
Pic1.png D:\Renamer3
('Pic1.png', 'D:/Renamer3\\car1.png')
Pic2.png D:\Renamer3
('Pic2.png', 'D:/Renamer3\\car2.png')
Pic3.png D:\Renamer3
('Pic3.png', 'D:/Renamer3\\car3.png')
Renamer3.py D:\Renamer3

"""

Maybe entering backslashes in your raw_input screws things up?

My input looks like this

'Enter a target folder'
c:/Documents/Mypictures/Test

'Enter a ragex'
.*jpg (this selects .jpg files and ignores the the other files i put in there such as .doc files. This is exactly what i hoped for)

'Enter a new base name'
Car

When i only use the print statement and not the

os.rename(fname, b)

I get this reply.

fname Column(Left) b column(Right)

'LambourghiniCar-Copy.jpg' 'Car1.jpg'
'FerrariCar.jpg' 'Car2.jpg'
'FordFiestaCar-Copy' 'Car3.jpg'

The names on the left (fname) are the original names.
The names on the right are what i want them changed to.

I know my script works perfectly up untill the line os.rename(fname, b) because i get the above reply.

I want the os.rename fuction to go out into the erlier specified folder, and swap from fname (shown above on the left) to b (shown above on the right)

I dont want to have to bring fname into the Renamer3 directory, as this would be a pain to the person using the program.

I want the program to go into the specified folder, in this case "c:/Documents/Mypictures/Test" just like it did to collect the values for "fname", and change the names there.

The problem is simply with the line

os.rename(fname, b)

i need it to look in the "targetfolder". The rest of the script does exactly what i want it to.

It may seem an arkward request, but my program is aimed to be user friendly, with minum work to the user. i will put a wx GUI interface and post my code for others to use once i am done, so when the comunity is done assisting me, i will be able to give them something back as a thanks.

According to your code fname is just the filename and b is the full pathname. Still, it will work with os.rename(fname, b).

My question was, what happens if someone enters a directory like this:
C:\temp\test
where \t will be looked on as a tab? Or your directory name has spaces, which Windows allows but has problems with.

True...

Im not sure how else to go about this, so that leaves me dead in the water to be honest.

OK, you want the code to look in the directory you specified for the files, but you never actually specify that the program should be running in that directory. You list files from the directory, but that isn't the same as changing directories.

Open up a command prompt on Windows for example. You'll be in c:\users\something. Now type dir c:\. What you get is a directory listing of the C root folder, but your working directory is still c:\users\something. You would need to issue the CD command to move into C root.

Well the same thing goes with Python. You have the user give you a foldername at line 7 and you check that it is a real folder. Let's ignore what vegaseat said about input validation for now. At line 18 you're doing the equivalent of DIR C:\, you're listing files from some directory, but that is not the working directory of the script.

My advise to you is to change line ten so that instead of breaking when you know you've got a good folder name, you change directory to it

os.chdir(targetfolder)

It Works, finl code for 2.6.4

Feel free to use it. thanks for all your help guys. the issues are sorted for now but im sure il need help with the input validation soon.

#!/usr/local/bin/python

import re, os

from os.path import join as pjoin, isdir
while True:
    targetfolder = raw_input('enter a target folder:\n').strip()
    os.chdir(targetfolder)
    break

newname = raw_input('enter a new base name:\n').strip()
newname = pjoin(targetfolder, newname)
rxin = raw_input('enter a regex to search for:\n').strip()
allowed_name = re.compile(rxin).match

a = 0

for fname in os.listdir(targetfolder):
    if allowed_name(fname):
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        os.rename(fname, b)
        print((fname, b))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.