"rbt" <[EMAIL PROTECTED]> wrote:
> Is there a recommended or 'Best Practices' way of checking the version of
> python before running
> scripts? I have scripts that use the os.walk() feature (introduced in 2.3)
> and users running 2.2
> who get errors. Instead of telling them, 'Upgrade you Python Install, I'd
> like to use sys.version
> or some other way of checking before running.
if you depend on os.walk, check for os.walk.
try:
from os import walk
except ImportError:
print "sorry, you need a newer python version!"
sys.exit()
or
import os
try:
os.walk
except AttributeError:
print "sorry, you need a newer python version!"
sys.exit()
if you only depend on a few functions, you can usually emulate them in
earlier versions. if 2.2 or newer is a reasonable requirement, you can
put a copy of the walk function in your script:
from __future__ import generators
try:
from os import walk
except ImportError:
def walk(...):
# copied from os.py in 2.3
if you want to support e.g 1.5.2 or newer, you can use something like this:
import os
try:
from os import walk
except ImportError:
class walk:
def __init__(self, directory):
self.stack = [directory]
def __getitem__(self, index):
dirpath = self.stack.pop(0)
dirnames = []
filenames = []
for file in os.listdir(dirpath):
name = os.path.join(dirpath, file)
if os.path.isdir(name) and not os.path.islink(name):
dirnames.append(file)
self.stack.append(name)
else:
filenames.append(file)
return dirpath, dirnames, filenames
(tweak as necessary)
</F>
--
http://mail.python.org/mailman/listinfo/python-list