Renaming OS files by file type in python

B

blur959

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
 
M

MRAB

blur959 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,044
Messages
2,570,388
Members
47,052
Latest member
ketan

Latest Threads

Top