Very newbie programming

TheSaint fc14301589 at
Sat Jun 10 17:44:23 CEST 2006


Is there a more pythonic way to implement the following program:


#! /usr/bin/env python

import os
import sys
a = os.listdir('/media')

# no mount dirs were found, exit nicely

if len(a) == 0:

# Maybe collecting the arguments via command line
# will make the program more flexible

mnt = open("/etc/mtab")
ptn = open("/proc/partitions")
dskt = '/home/user/Desktop/'

c =[]

# Filling the c with the list of devices which are recorded to be mounted

d = filter((lambda a: a[:2] =='/d'),mnt.readlines()) # non /dev-mounts are
d = map((lambda a: a.split()[:1]),d) # only the first info column is used

[c.append(str(a)[2:-2]) for a in d]

# checking against /proc/partitions to see if there're mountpoints without
# corresponding partition which has been removed from the list
#  ALL mountpoints with available partition will be removed from the list

x = ptn.readlines()
for a in x[2:]:
        b = a.split()[-1]
        for d in c:
                if b == d.rsplit('/',1)[-1]:

if len(c) != 1:
        sys.exit(0) # if none or more than one match exit

cmnd = str(c)[2:-2]
err = os.system('umount ' + cmnd)
a = os.listdir(dskt)

#retrieve the info from the KDE desktop icons

for i in a:
        if 'desktop' not in i: continue
        y = open(dskt + i)
        d = y.readlines()
        for x in d:
                if 'URL=/media' not in x: continue
                icon = i
                dvc = x[11:-1]

err += os.system('rm -f ' + dskt + icon)
err += os.system('rmdir /media/' + dvc)
# if something went wrong exit with high number

I still have a long learnig curve, please give some advise in order to make
the program a bit faster o nicer :)


More information about the Python-list mailing list