Just like in the title, instead of renaming the files in the "target folder" it just renames all possible files in the python directory, and im not sure how to fix it, although this is my best attempt.

#!usr/local/bin/python

import re, os

from os.path import join as pjoin
targetfolder = raw_input('enter a target folder:\n').strip()
newname = raw_input('enter a new base name:\n')
newname = pjoin(targetfolder, newname)

rxin = raw_input('enter a ragex to search for:\n')
foo = re.compile(rxin)
newname = raw_input('enter a new base name:\n')
a = 0
for fname in os.listdir(os.getcwd()):
    allowed_name = re.compile(rxin).match
    if allowed_name(fname):
        # newfname = string.lower(re.sub(foo,
                                             # '', fname))
        # b = (newname + str(a))
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        os.rename(fname, b)

I think that line 14 might be your problem. Even though you ask the user for a path on line 5, you perform your renaming on the operating systems current working directory (CWD) on line 14. I think you should try changing line 14 to

for fname in os.listdir(targetfolder):

I modified your code so that it prints the file name and the destination name instead of renaming the file. Run it and modifiy it until it gives the correct values, then uncomment the 'rename' line.

#!/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(os.getcwd()):
    if allowed_name(fname):
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        #os.rename(fname, b)
        print((fname, b))

Thanks guys, you've both helped alot, i thought that by entering a destination folder that would have been my cwd.

I can make it work with the information you supllied.

CWD is the directory that your script is running in. If you want to change the CWD that your script is using, you can use os.chdir()

Line 7 of your original script would change to
os.chdir(raw_input('Enter directory: '))

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.
#!/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, 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)
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.