Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Renaming OS files by file type in python

Reply
Thread Tools

Renaming OS files by file type in python

 
 
blur959
Guest
Posts: n/a
 
      08-12-2010
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

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      08-12-2010
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get exclusive file lock when safely renaming a file Steven D'Aprano Python 3 12-06-2008 09:42 PM
Does file renaming always fail if the file is shared? Chris Java 6 03-06-2007 06:08 PM
Batch File - Renaming File with DATE Tom Computer Support 11 03-05-2007 10:33 PM
how to detect file creating and deleting and renaming files immediately in java master007 Java 8 03-06-2006 07:30 PM
Renaming files in the flash. AM Cisco 0 08-17-2005 05:08 PM



Advertisments