euglug.org

[Eug-lug] Python help – more elegant way to do this…

작성자

카테고리:



Quentin HartmanMon Mar 27 07:57:42 PST 2006

On 3/27/06, Martin Kelly <aomighty at gmail.com> wrote:
>
> Hi, I was creating a python script to rename all files in a directory as
> specified by a given pattern. I know there are already programs that can
> do this, but I am doing this as a learning exercise.
>
> The format for the command is like this:
> rename /home foo bar
> ... to change everything in /home with "foo" in it to "bar"
>
> I got it working, but it's rather messy, and I was wondering if there's
> a more elegant way to do it; after all, it is python :).
>
> The .py file is attached.


On first glance it looks pretty OK. I am not a python guru, but
algorithmically I don't see anything immediately that could be optimized.
Syntactically you can likely "compress" it, but I don't really see the value
in that. You might consider using the regex module to make it more flexible.
As it sits, it seems it would not match on mixed case, partial renames
(moving 123foo.txt to 123bar.txt for instance), and would likely break on
files with spaces in them.

A bit of an aside here, but what is a "topectory"? I gathered from context
that it is the top-level directory that you are working on... but, yeah. Not
even google has ever seen "topectory" before. Personally, I have to spend a
lot of time at work training people the correct terms for things. There have
been a lot of local colloquialisms developed here around technology and it
is a real hindrance to communication when people work with others who do not
know them. If they had been taught the recognized terms for things right
from the get go, they would have been better off in the long term, even if
it was marginally more difficult to teach them those terms initially. Using
"real" terminology whenever you can is definitely a good practice and will
limit headaches down the road.

--
-Regards-

-Quentin Hartman-
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.euglug.org/pipermail/euglug/attachments/20060327/6106f8e4/attachment.htm