[Courses] [python] Lesson 8: Extras
leslie.brothers at verizon.net
Sun Aug 7 19:16:41 UTC 2011
On Fri, 2011-08-05 at 13:38 -0700, Akkana Peck wrote:
> Lesson 8: Extras
For my first bit of homework, I decided to write a python script to
automate a series of shell commands I routinely do.
The following program (1) makes a new directory; (2) goes to my home
directory and removes an old soft link; (3) creates a new link using the
path to the new directory.
(Background is I get tutoring every few weeks. I keep my exercises in
directories named for the date of my next lesson. I have a soft link in
my home directory called 'next.lesson' that takes me right to the
directory for the upcoming lesson. After every lesson, I update this
soft link, in order to go to the new directory for the next lesson's
date as soon as I open the terminal and type 'cd next.lesson'. The
program which follows must be run from the "Lessons" parent directory.)
My objectives were to try to use os methods, static methods, and
exception handling. I initially wrote this as a series of straight
commands, which worked fine, but I made it more complicated just to
practice doing those class methods. I didn't get around to optional
arguments or subprocesses in this one -- I'll try those next!
While looking up one of the expressions used in Akkana's lesson,
os.getenv(), I discovered a host of other os methods, including
os.chdir(), which was just what I needed. It made me think there must
also be os.mkdir() and os.rmdir() and I found out those work too. (I
also found os.uname() which gives some cool output.)
Here's the program:
print "New directory is ",d+"\n"
print "Previous link in user directory removed.\n"
newpath = "/Desktop/Lessons/"+d
os.system("ln -s %s next.lesson" % newpath)
print "New 'next.lesson' link created."
q = raw_input("Name of new directory?\n")
except Exception, err:
print "An error occurred.", err
p = raw_input ("Press 'q' if you are done; press 'enter' if you want to
see the link in your directory.\n")
if p != 'q':
> Courses mailing list
> Courses at linuxchix.org
More information about the Courses