473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

First practical Python code, comments appreciated

Hi All,

I've written my first piece of practical Python code (included below),
and would appreciate some comments. My situation was that I had a
directory with a number of subdirectories that contained one or more
zip files in each. Many of the zipfiles had the same filename (which is
why they had previously been stored in separate directories). I wanted
to bring all of the zip files (several hundrd in total) down to the
common parent directory and obviously rename them in the process by
appending a unique string to each filename.

The code below did the job admirably, but I don't imagine it is
anywhere near as efficiently written as it could and should be. Note:
I'm aware there was no need, for example, to wrap the "os.walk(path)"
statement in a def -- in a couple of places I added extra steps just to
help familiarize myself with Python syntax.

I'd very much like to see how experienced Python coders would have
achieved the same task, if any of you can spare a couple of minutes to
share some knowledge with me.

Many thanks and much warmth,

planetthoughtful

import os
fcount = 0
path = 'X:\zipfiles'
def listFiles(path):
mylist = os.walk(path)
return mylist

filelist = listFiles(path)
for s in filelist:
if len(s[2]) > 0:
for f in s[2]:
pad = str(fcount)
if len(pad) == 1:
pad = "00" + pad
elif len(pad) == 2:
pad = "0" + pad

(fname, fext) = os.path.splitext(f)
oldname = f
newname = fname + "_" + pad + fext
os.rename(s[0] + "\\" + oldname, path + "\\" + newname)
fcount = fcount + 1

Dec 14 '05 #1
6 1649
planetthoughtful wrote:
Hi All,

I've written my first piece of practical Python code (included below),
and would appreciate some comments. My situation was that I had a
directory with a number of subdirectories that contained one or more
zip files in each. Many of the zipfiles had the same filename (which is
why they had previously been stored in separate directories). I wanted
to bring all of the zip files (several hundrd in total) down to the
common parent directory and obviously rename them in the process by
appending a unique string to each filename.

The code below did the job admirably, but I don't imagine it is
anywhere near as efficiently written as it could and should be. Note:
I'm aware there was no need, for example, to wrap the "os.walk(path)"
statement in a def -- in a couple of places I added extra steps just to
help familiarize myself with Python syntax.

I'd very much like to see how experienced Python coders would have
achieved the same task, if any of you can spare a couple of minutes to
share some knowledge with me.

Many thanks and much warmth,

planetthoughtful
Brave of you. Please note I haven't actually tested the exact format of
these suggestions, so I made have made stupid typos or syntax errors.
import os
fcount = 0
path = 'X:\zipfiles'
def listFiles(path):
mylist = os.walk(path)
return mylist

filelist = listFiles(path)
for s in filelist:
if len(s[2]) > 0:
You don't really need this "if" - with an empty s the "for" loop on the
next line will simply execute its body zero times, giving the effect you
appear to want without the extra level of logic.
for f in s[2]:
pad = str(fcount)
if len(pad) == 1:
pad = "00" + pad
elif len(pad) == 2:
pad = "0" + pad
Here you could take advantage of the string formatting "%" operator and
instead of the "if" statement just say

pad = "%03d" % fcount
["%03d" % x for x in (1, 3, 10, 30, 100, 300)]

['001', '003', '010', '030', '100', '300']
(fname, fext) = os.path.splitext(f)
oldname = f
There isn't really any advantage to this assignment, though I admit it
does show what you are doing a little better. So why not just use

for oldname in s[2]:

to control the loop, and then replace the two statements above with

(fname, fext) = os.path.splitext(oldname)

Note that assignments of one plain name to another are always fast
operations in Pythin, though, so this isn't criminal behaviour - it just
clutters your logic a little having essentially two names for the same
thing.
newname = fname + "_" + pad + fext
os.rename(s[0] + "\\" + oldname, path + "\\" + newname)
That form is non-portable. You might argue "I'm never going to run this
program on anything other than Windows", and indeed for throwaway
programs it's often easier to write something non-portable. It's
surprising, though, how often you end up *not* throwing away such
programs, so it can help to write portably from the start. I'd have used

newname = os.path.join(path,
"%s_%s.%s" % (fname, pad, fext))
os.rename(os.path.join(s[0], oldname), newname)
fcount = fcount + 1


Just a few pointers to make the script simpler, but your program is a
very creditable first effort. Let us know what mistakes I made!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 14 '05 #2
"Steve Holden" <st***@holdenweb.com> wrote in message
news:ma***************************************@pyt hon.org...
That form is non-portable. You might argue "I'm never going to run this
program on anything other than Windows", and indeed for throwaway
programs it's often easier to write something non-portable. It's
surprising, though, how often you end up *not* throwing away such
programs, so it can help to write portably from the start. I'd have used

newname = os.path.join(path,
"%s_%s.%s" % (fname, pad, fext))
os.rename(os.path.join(s[0], oldname), newname)
fcount = fcount + 1


Just a few pointers to make the script simpler, but your program is a
very creditable first effort. Let us know what mistakes I made!


Just to chime in, and say that Steve's comments on portable programming have
to do with more than just your code running on other machines. Developing
habits such as portable programming helps you to do these kinds of things
naturally, rather than to have to make a special effort if/when the issue
does come up. Meanwhile, your portfolio of developed code reflects a
broader thinking span on your part, beyond just "let me whip together
something quick for the problem at hand." When presenting your work, at a
conference or a job interview, it always helps to convey that you can see
the bigger picture.

Also, developing portable coding habits makes it easier for *you* to
assimilate code that others may have written for other platforms -
portability is an n-way street. If you download some code fragment from
SourceForge, or from a tutorial, or a PyCon presentation, you will be less
likely to scratch your head over the purpose of os.join if you are in the
habit of using it already.

Otherwise, your code (with Steve's comments) is good, and it looks like it
does the job. But I would also look over some tutorials, or examples of
existing code - Python comes with a huge set of sample code in the provided
libraries, and if you simply "self-teach" yourself, you can develop some bad
habits, and remain ignorant of some nice features and best practices.

Look at the use of native data structures (tuples, lists, and dicts), the
use of classes and class hierarchies, the uses of the module library, and
some of the relatively recent idioms of generators, list
comprehensions/generator expressions. Look at the layout of the code to see
how the author broke the problem into manageable pieces. Look for some of
the modules that crop up over and over again - not just sys, os, and math,
which are analogous to the C RTL, but also itertools, which I think is more
in the category of useful magic, like the C++ STL's <algorithm> module.

Look for object design concepts, such as containment and delegation, and ask
yourself how well they fit the problem for the given script. If you are new
to object-oriented coding as well, read some of the free online Python
introductory texts, such as "Dive into Python" and "Think like a Computer
Scientist in Python," to learn how some of the object design idioms
translate to Python's type and language model.

Python expertise comes in a series of plateaus, over time and with practice.
It's a stimulating and rewarding journey, no matter which plateau you
reach - welcome to Python!

-- Paul
Dec 14 '05 #3
Thanks to both Steve and Paul!

I actually come from a PHP background, and I'm learning Python, oddly
enough, as a result of recently purchasing a USB Flash Drive, and
through wanting to be able to carry a portable programming language on
the drive so that I have the capability of developing / using useful
code wherever I happen to be (I'm actually using Movable Python
http://www.voidspace.org.uk/python/movpy/ - I hope it's not lacking in
any fundamental way).

To be honest, I could have achieved the same result more gracefully in
PHP (and I know that will cause some eyebrows to arch over the
suggestion that anything can be done gracefully in PHP), but that's
simply because I'm very comfortable with its syntax and with writing
economical code in it, not because it's actually well-suited to that
type of task.

I'm already working through Dive Into Python, which seems to be a good
starting place. Thankfully, I'm comfortable with OO concepts, at least
in how they are expressed in PHP5, so I'm not entirely lost in
unfamiliar territory with those aspects of Python.

My big learning curve will come, I suspect, when I move into creating
GUI apps with Python and wxPython, since that's my end goal - to be
able to carry several self-developed applications on my Flash Drive
that aren't dependent on any resources not found on the Flash Drive.

But, you have to learn to crawl before you can learn to code, so
approaching basic tasks like the one in my first attempt at a practical
application of Python are a good way to begin.

Thanks to both for your comments and advice!

Much warmth,

planetthoughtful

Dec 14 '05 #4
Paul McGuire wrote:
[...]
portability is an n-way street.

+1 QOTW

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 14 '05 #5
Paul McGuire wrote:
[...]
portability is an n-way street.

+1 QOTW

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 14 '05 #6
planetthoughtful wrote:
Hi All,

I've written my first piece of practical Python code (included below),
and would appreciate some comments. My situation was that I had a
directory with a number of subdirectories that contained one or more
zip files in each. Many of the zipfiles had the same filename (which is
why they had previously been stored in separate directories). I wanted
to bring all of the zip files (several hundrd in total) down to the
common parent directory and obviously rename them in the process by
appending a unique string to each filename.

The code below did the job admirably, but I don't imagine it is
anywhere near as efficiently written as it could and should be. Note:
I'm aware there was no need, for example, to wrap the "os.walk(path)"
statement in a def -- in a couple of places I added extra steps just to
help familiarize myself with Python syntax.

I'd very much like to see how experienced Python coders would have
achieved the same task, if any of you can spare a couple of minutes to
share some knowledge with me.

Many thanks and much warmth,

planetthoughtful

import os
fcount = 0
path = 'X:\zipfiles'
def listFiles(path):
mylist = os.walk(path)
return mylist
Note that os.walk() doesn't return a list, it returns an iterable object (a generator).
Your usage will work for either, but your names are misnomers.
filelist = listFiles(path)
for s in filelist:
I would write
for dirpath, dirnames, filenames in filelist:

which gives names to what you call s[0] and s[2]

or just
for dirpath, dirnames, filenames in os.walk(path):

Kent
if len(s[2]) > 0:
for f in s[2]:
pad = str(fcount)
if len(pad) == 1:
pad = "00" + pad
elif len(pad) == 2:
pad = "0" + pad

(fname, fext) = os.path.splitext(f)
oldname = f
newname = fname + "_" + pad + fext
os.rename(s[0] + "\\" + oldname, path + "\\" + newname)
fcount = fcount + 1

Dec 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Mediocre Person | last post by:
Well, after years of teaching grade 12 students c++, I've decided to make a switch to Python. Why? * interactive mode for learning * less fussing with edit - compile - link - run - debug -...
1
by: avinashc | last post by:
If anyone is interested in a /etc/hosts.deny automatic update script (Unix only) based on sshd/vsftpd attacks, here's a python script: http://www.aczoom.com/tools/blockhosts/ This is a beta...
2
by: Hal Vaughan | last post by:
I'm self taught and most of what I've been working on for the past several years has been entirely in Perl and Java. I've noticed that I can code about 5 times faster in Perl than Java, in part...
27
by: hokiegal99 | last post by:
Hi Guys, This is my first C program. I started programming in Python. Please look over this source and let me know if I'm doing anything wrong. I want to start out right with C, so if you see...
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
5
by: Carl J. Van Arsdall | last post by:
Hey everyone, another question for the list. In particular i'm looking for comments on some of the distributed technologies supported in python. Specifically, I'm looking at XML-RPC, RPyC, CORBA,...
23
by: IOANNIS MANOLOUDIS | last post by:
I want to learn python. I plan to buy a book. I always find printed material more convenient than reading on-line tutorials. I don't know PERL or any other scripting language. I only know some...
6
by: Lex Hider | last post by:
Hi, Apologies if this is against etiquette. I've just got my first python app up and running. It is a podcast aggregator depending on feedparser. I've really only learnt enough to get this up and...
20
by: Shawn Milo | last post by:
I'm new to Python and fairly experienced in Perl, although that experience is limited to the things I use daily. I wrote the same script in both Perl and Python, and the output is identical. The...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.