From: Dave Angel on
blur959 wrote:
> On Aug 9, 6:01 pm, Chris Rebert <c...(a)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

From: MRAB on
Dave Angel wrote:
> blur959 wrote:
>> On Aug 9, 6:01 pm, Chris Rebert <c...(a)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. :-)
From: Dave Angel on


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


From: Chris Rebert on
On Mon, Aug 9, 2010 at 3:19 AM, Peter Otten <__peter__(a)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
From: Peter Otten on
Chris Rebert wrote:

> On Mon, Aug 9, 2010 at 3:19 AM, Peter Otten <__peter__(a)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