Re: simple renaming files program

2010-08-09 Thread Chris Rebert
On Mon, Aug 9, 2010 at 2:44 AM, blur959 blur...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple renaming files program

2010-08-09 Thread Shashwat Anand
On Mon, Aug 9, 2010 at 3:14 PM, blur959 blur...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple renaming files program

2010-08-09 Thread Ulrich Eckhardt
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

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


Re: simple renaming files program

2010-08-09 Thread Peter Otten
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple renaming files program

2010-08-09 Thread blur959
On Aug 9, 6:01 pm, Chris Rebert c...@rebertia.com wrote:
 On Mon, Aug 9, 2010 at 2:44 AM, blur959 blur...@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!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple renaming files program

2010-08-09 Thread Dave Angel

blur959 wrote:

On Aug 9, 6:01 pm, Chris Rebert c...@rebertia.com wrote:
  

snip
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 =aw_input(input file directory)
s =aw_input(search for name)
r =aw_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!

  
A refinement:  use os.path.join(), rather than just join().  It's 
smarter about adding the right kind of slash between the nodes, if 
needed.  Currently, if you leave off the trailing slash (from 
directory), you'll end up with the files being one level up, and the 
individual files having a string prepended.


DaveA

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


Re: simple renaming files program

2010-08-09 Thread MRAB

Dave Angel wrote:

blur959 wrote:

On Aug 9, 6:01 pm, Chris Rebert c...@rebertia.com wrote:
 

snip
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 =aw_input(input file directory)
s =aw_input(search for name)
r =aw_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!

  
A refinement:  use os.path.join(), rather than just join().  It's 
smarter about adding the right kind of slash between the nodes, if 
needed.  Currently, if you leave off the trailing slash (from 
directory), you'll end up with the files being one level up, and the 
individual files having a string prepended.



Have a look at the imports, Dave. :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: simple renaming files program

2010-08-09 Thread Dave Angel



MRAB wrote:

snip

from os.path import isdir, join
snip



Have a look at the imports, Dave. :-)

Oops.  I should have noticed that it was a function call, not  a 
method.  And there's no built-in called join().  I just usually avoid 
using this kind of alias, unless performance requires.


thanks for keeping me honest.

DaveA


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


Re: simple renaming files program

2010-08-09 Thread Chris Rebert
On Mon, Aug 9, 2010 at 3:19 AM, Peter Otten __pete...@web.de wrote:
 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.

Er, unless I'm really missing something, that's what my code already
does. Perhaps you misread it? The replace() clearly happens before the
2nd join(). I took special care to account for and avoid the potential
problem you're talking about.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple renaming files program

2010-08-09 Thread Peter Otten
Chris Rebert wrote:

 On Mon, Aug 9, 2010 at 3:19 AM, Peter Otten __pete...@web.de wrote:

 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.
 
 Er, unless I'm really missing something, that's what my code already
 does. Perhaps you misread it? The replace() clearly happens before the
 2nd join(). I took special care to account for and avoid the potential
 problem you're talking about.

You're right. Sorry for the confusion.

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