I started writing a python script to rename files. I wanted it to be able to rename multiple files in a given directory. I started off trying to use rename but then found other options Can't quite get it to happen.

Hope my code is understandable, can anyone assist in why it doesn't work?

""" To take input from user (directory and characters) read files in a directory and
remove characters(word) from the filenames in that directory  
so for c:\somedirectory\lovelyname_annoying_chars.txt return
c:\somedirectory\lovelyname.txt
input input --> dir/name.ext"""

import os
# get the directory from user
targetfolder = raw_input('enter a target folder:\n').strip()
# get characters to remove
charArgs = raw_input('What Characters to remove: ')
# read all filenames in directory
filenames = walk(targetfolder,names)
def removechars(*names):
	# use os.path.splitext() function to remove file extension
	a = names.splitext()
	# split names at point where charArgs occur
	b = a.split(charArgs)
# Rejoin new filename and extension 
newname = join(a, b)
write newname

Your problem is not clearly stated. Do you want to handle all files in the specified directory, or in and below the specified directory?

On line 13, I expect NameError: name 'walk' is not defined It needs to be os.walk() ... but if you only want files in the exact directory, you want to use os.listdir() instead.

Is line 14 what you really intend? Do you want to pass every file name as an individual argument to function removechars() ? I think you intend to pass the list and then work your way though the list, no? In which case lose the asterisk.

Assuming you work out that problem, your line 16 is wrong. The function is os.path.splitext() and it returns a pair/tuple of strings not just one.

Similar problem on line 18: You can indeed say name.split(charArgs), but you will get back a list of one or more substrings, not a single string. And I'm not sure you actually want to split on the full set of charArgs (Maybe you do: I can't figure out what your specification is)

line 20 is wrong too. You just want to concatenate the new basename and the old extension, so if you had created basename, extension up at line 16, then down here you would write newname = newbasename + extension line 21 is wrong too. This is where os.rename() comes in, not write() .

Start small. First learn to split and do fixed change to file name. Just print the result. Then change the file name to be element from fixed list of file names using for loop... This is bottom up implementation. Usually it is easier to design top down and implement bottom up preferably using functions and then dividing the longer programs to modules and/or objects.

Well Gris has cooked the food well enough for you. :)

Your problem is not clearly stated. Do you want to handle all files in the specified directory, or in and below the specified directory?

On line 13, I expect NameError: name 'walk' is not defined It needs to be os.walk() ... but if you only want files in the exact directory, you want to use os.listdir() instead.

Is line 14 what you really intend? Do you want to pass every file name as an individual argument to function removechars() ? I think you intend to pass the list and then work your way though the list, no? In which case lose the asterisk.

Assuming you work out that problem, your line 16 is wrong. The function is os.path.splitext() and it returns a pair/tuple of strings not just one.

Similar problem on line 18: You can indeed say name.split(charArgs), but you will get back a list of one or more substrings, not a single string. And I'm not sure you actually want to split on the full set of charArgs (Maybe you do: I can't figure out what your specification is)

line 20 is wrong too. You just want to concatenate the new basename and the old extension, so if you had created basename, extension up at line 16, then down here you would write newname = newbasename + extension line 21 is wrong too. This is where os.rename() comes in, not write() .

So to clear specification up I just wanted to read all files in current directory so os.listdir() is the better option.

I intended to split the string at the point where charArgs occurred so in process this is what I was thinking. Starting with:

lovelyname_annoying_chars.txt
# split the name and the extension
(lovelyname_annoying_chars, txt)
# take the name and split where annoying chars start.
(lovelyname, _annoying_chars)
# join the lovelyname and the extension as my output
lovelyname.txt

Most of what you have written is very helpful and makes sense I just a little more time to review and think it over and have another go.

Thanks for the help much appreciated.

Look into regular expression for changing filename.
Here is an example.

import re

files = '''\
lovelyname_annoying_chars.txt
Diename_annoying_charshard.txt
somenamename_annoying_chars.txt'''

new_files = re.sub(r'name_annoying_chars', '', files)
print new_files

"""Output-->
lovely.txt
Diehard.txt
somename.txt
"""
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.