simple renaming files program

B

blur959

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
 
C

Chris Rebert

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
 
U

Ulrich Eckhardt

blur959 said:
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
 
P

Peter Otten

Chris said:
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
 
B

blur959

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!
 
D

Dave Angel

blur959 said:
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
 
M

MRAB

Dave said:
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. :)
 
D

Dave Angel

MRAB said:
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
 
C

Chris Rebert

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
 
P

Peter Otten

Chris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top