From: blur959 on
Hi all, I am creating a program that renames all files of the similar
file type. But i am stuck at this part. I tried running this code and
I got this error: new_name = os.rename(path, newpath)
WindowsError: [Error 183] Cannot create a file when that file already
exists. Hope you guys could help.



import os

directory = raw_input("Please input file directory. \n\n")

s = raw_input("Please input a name to replace. \n\n")
ext = raw_input("input file ext")

for filename in listdir(directory):
if ext in filename:
path = join(directory, filename)
fnpart = os.path.splitext(filename)[0]
replace_name = filename.replace(fnpart, s)
newpath = os.path.join(directory, replace_name)

new_name = os.rename(path, newpath)
print new_name

From: MRAB on
blur959 wrote:
> Hi all, I am creating a program that renames all files of the similar
> file type. But i am stuck at this part. I tried running this code and
> I got this error: new_name = os.rename(path, newpath)
> WindowsError: [Error 183] Cannot create a file when that file already
> exists. Hope you guys could help.
>
[snip]
As the traceback says, a file with that new name already exists. You
can't have 2 files with the same name in the same folder.