simple renaming files program

Ulrich Eckhardt eckhardt at satorlaser.com
Mon Aug 9 06:11:06 EDT 2010


blur959 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

I see that you are using os.listdir(), so the files should be present, but
still, I would consider checking that when I encounter this error.

> 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

Looks good so far, but what are the values in s, r, file and the result of
file.replace(s, r) for the case that fails? Also, as a side note,
help(os.rename) doesn't document any returnvalue to store in n, but that
doesn't seem to be the problem.

Uli


-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list