From: blur959 on
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 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
From: Chris Rebert on
On Mon, Aug 9, 2010 at 2:44 AM, blur959 <blur959(a)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
>
> 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

os.rename() takes paths that are absolute (or possibly relative to the
cwd), not paths that are relative to some arbitrary directory (as
returned by os.listdir()).
Also, never name a variable "file"; it shadows the name of the built-in type.

Hence (untested):
from os import listdir, rename
from os.path import isdir, join
directory = raw_input("input file directory")
s = raw_input("search for name")
r = raw_input("replace name")

for filename in listdir(directory):
path = join(directory, filename) #paste the directory name on
if isdir(path): continue #skip subdirectories (they're not files)
newname = filename.replace(s, r)
newpath = join(directory, newname)
n = rename(path, newpath)
print n

Cheers,
Chris
--
http://blog.rebertia.com
From: Ulrich Eckhardt on
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

From: Peter Otten on
Chris Rebert wrote:

> Hence (untested):
> from os import listdir, rename
> from os.path import isdir, join
> directory = raw_input("input file directory")
> s = raw_input("search for name")
> r = raw_input("replace name")
>
> for filename in listdir(directory):
> path = join(directory, filename) #paste the directory name on
> if isdir(path): continue #skip subdirectories (they're not files)
> newname = filename.replace(s, r)
> newpath = join(directory, newname)
> n = rename(path, newpath)
> print n

Warning: I don't remember how Windows handles this, but unix will happily
perform os.rename("alpha/alpha.txt", "beta/beta.txt") and overwrite
beta/beta.txt with alpha/alpha.txt.

I'd rather modify the filename before joining it with the directory.

newname = filename.replace(s, r)
if newname != filename:
path = os.path.join(directory, filename)
newpath = os.path.join(directory, newname)
os.rename(path, newpath)

If you don't you run the risk of operating in unexpected directories.

Peter
From: blur959 on
On Aug 9, 6:01 pm, Chris Rebert <c...(a)rebertia.com> wrote:
> On Mon, Aug 9, 2010 at 2:44 AM, blur959 <blur...(a)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
>
> > 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
>
> os.rename() takes paths that are absolute (or possibly relative to the
> cwd), not paths that are relative to some arbitrary directory (as
> returned by os.listdir()).
> Also, never name a variable "file"; it shadows the name of the built-in type.
>
> Hence (untested):
> from os import listdir, rename
> from os.path import isdir, join
> directory = raw_input("input file directory")
> s = raw_input("search for name")
> r = raw_input("replace name")
>
> for filename in listdir(directory):
>     path = join(directory, filename) #paste the directory name on
>     if isdir(path): continue #skip subdirectories (they're not files)
>     newname = filename.replace(s, r)
>     newpath = join(directory, newname)
>     n = rename(path, newpath)
>     print n
>
> Cheers,
> Chris
> --http://blog.rebertia.com



Thanks, they worked!