expanding a variable
kalle at gnupung.net
Thu Mar 22 22:33:49 CET 2001
Sez Jonathan Soons:
> I am trying to give my cv group (who all have logins like cv???.)
> all the same .profile. The variable in the last line of the script
> doesn't seem to be expanding, even though this is very similar to
> an example in "Core Python Progr..."
Note: I haven't read the book, so I don't know the example.
> d = commands.getoutput('ls /u')
> dirs = string.split(d, '\n')
> for dir in dirs:
> if re.search('^cv', dir):
This could be expressed as
if dir[:2] == "cv":
or (with Python 1.6 and up)
if dir.search("cv") == 0:
I don't know which of the three is best, but I would use the slice and
string compare (my first).
> os.system('cp /u/test/.profile /u/dir/')
os.system('cp /u/test/.profile /u/%s/' % dir)
os.system('cp /u/test/.profile /u/' + dir + '/')
> Should I stick to bash for this kind of thing?
Nah, I don't think so. I use python for almost all my scripting.
BTW, check out the standard module "shutil".
Email: kalle at gnupung.net | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
[ Not signed due to lossage. Blame Microsoft Outlook Express. ]
More information about the Python-list