simple renaming files program

Shashwat Anand anand.shashwat at gmail.com
Mon Aug 9 12:01:37 CEST 2010


On Mon, Aug 9, 2010 at 3:14 PM, blur959 <blur959 at hotmail.com> wrote:

> Hi, all, I am working on a simple program that renames files based on
> the directory the user gives, the names the user searched and the
> names the user want to replace. However, I encounter some problems.
> When I try running the script, when it gets to the os.rename part,
> there will be an error. The error is :
>  n = os.rename(file, file.replace(s, r))
> WindowsError: [Error 2] The system cannot find the file specified
>

This is because the file does not exist.


>
> I attached my code below, hope you guys can help me, Thanks!
>
> import os
> directory = raw_input("input file directory")
> s = raw_input("search for name")
> r = raw_input("replace name")
>
> for file in os.listdir(directory):
>    n = os.rename(file, file.replace(s, r))
>    print n
>

Tried this on my system, works.
>>> [shutil.move(i, r) for i in os.listdir(directory) if i==s]

--
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100809/06b1eed2/attachment.html>


More information about the Python-list mailing list