[Tutor] Comments appreciated
Luis N
tegmine at gmail.com
Sun Dec 26 18:56:01 CET 2004
Hi,
Jeff Shanon, thanks for your help. I was wondering if there was a
function to automatically get the user on a *nix system, and had tried
the if __name__ == __main__ but didn't really get it until your
example. The only thing I'm not clear about is how 'trashcan' can be a
local variable inside main() when it's required by both trash() and
can()
The only thing that's missing is that this script can't handle paths
like ~/dir/junkthis
Would a regular expression be the best way of finding the last /
followed by some text to be able to chdir to junk files from another
location?
#!/usr/local/bin/python
import os.path
from os import mkdir, remove
from sys import argv
from shutil import rmtree
trashcan = os.path.expanduser("~/.trashcan")
def main(junk):
empty = False
if "-e" in junk:
empty = True
junk.remove("-e")
if not os.path.exists(trashcan):
os.mkdir(trashcan)
if len(junk) > 0:
trash(junk)
if empty:
can()
def trash(junk):
for i in junk:
toss = trashcan + "/" + i
if os.path.exists(toss):
if os.path.isdir(toss):
rmtree(toss)
if os.path.isfile(toss):
os.remove(toss)
os.rename(i, toss)
def can():
for j in os.listdir(trashcan):
toss = trashcan + "/" + j
if os.path.isdir(toss):
rmtree(toss)
if os.path.isfile(toss):
os.remove(toss)
if __name__ == '__main__':
main(argv[1:])
More information about the Tutor
mailing list