Newbie Here
Kent Johnson
kent37 at tds.net
Wed Jun 1 08:13:39 EDT 2005
Mark Sargent wrote:
> Hi All,
>
> I'm taking the plunge into Python. I'm currently following this tutorial,
> http://docs.python.org/tut/
> I am not a programmer in general, although I've learnt a bit of bash
> scripting and some php/asp. I want to get into python to use it for
> Linux/Unix related stuff. A question I have, is, those of you who use it
> for the same things, what do you primarily use it for. Could you show me
> some examples.? I find the hardest thing with programming, is not
> remember the syntax/logic etc, but, when to use it. Perhaps that is also
> a personal thing, but, I'd love to see some basic examples out there.
As others have said, Python is very useful for a wide range of tasks. But you asked for simple
examples. One thing I use Python for is simple file manipulations. I recently had to look through
2,400 folders to see if they contained two specific files in a nested subfolder. This is easy to do
in Python; below is my script.
Kent
''' Look in the warehouse for courses that are missing
\output\html\saveres.htm and/or \output\html\readres.htm
path module from http://www.jorendorff.com/articles/python/path/
'''
import path, sys
def checkCourses(basePath, out):
''' Iterate through the course / output / html folders rooted at basePath
looking for missing files.
'''
for coursePath in basePath.dirs():
htmlPath = coursePath / 'output' / 'html'
if not htmlPath.exists():
continue
for fileName in [ 'saveres.htm', 'readres.htm' ]:
aPath = htmlPath / fileName
if not aPath.exists():
print >>out, aPath
print >>out
basePath = path.path(r'\\Amrnasfs1\QA\BS_and_SIMS\Content')
out = open('MissingFiles.txt', 'w')
checkCourses(basePath, out)
More information about the Python-list
mailing list