Shell tool
Hello all, I’ve just started trying to use Python to automate Windows system administration. I wrote my first (long-winded, amateurish) shell-tool to os.path.walk through a directory structure and delete files older than 2 days. (Keeps users from cluttering some temporary storage space). It works fine, except that it does not delete the empty directories. Any hints? """Delete files older than AGING constant using os.path.walk""" import sys, os, time AGING = 172800 # Default 2 days def lister(dummy, dirName, filesInDir): print '[' + dirName + ']' for fname in filesInDir: path = os.path.join(dirName, fname) if not os.path.isdir(path): print path, time.ctime(os.stat(path).st_mtime), fileage(path), prune(path) purge(path) def fileage(file): curtime = time.time() modtime = os.stat(file).st_mtime age = curtime - modtime return age def prune(file): if fileage(file) > AGING: return ("T") else: return ("File should not be deleted") def purge(file): if prune(file) == "T": os.remove(file) if __name__ == '__main__': os.path.walk(sys.argv[1], lister, None) --Adam Getchell
RE: Shell tool
Looks like you're only deleting files, with the "if not os.path.isdir(path)" line. But beyond that, I think there's a problem with the order in which os.path.walk will call your lister. walk() will call lister from top-down, whereas a delete type function like this needs to walk from bottom up. Problem simpler to write your own little recursive thingy. Or, you could cheat. Take out the directory check mentioned above Attempt to delete all files, dir or file alike. Catch and ignore the 'directory not empty' errors Keep looping from the top until there's nothing left. -Original Message-From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Adam GetchellSent: Sunday, May 12, 2002 1:50 AMTo: [EMAIL PROTECTED]Subject: Shell tool Hello all, I’ve just started trying to use Python to automate Windows system administration. I wrote my first (long-winded, amateurish) shell-tool to os.path.walk through a directory structure and delete files older than 2 days. (Keeps users from cluttering some temporary storage space). It works fine, except that it does not delete the empty directories. Any hints? """Delete files older than AGING constant using os.path.walk""" import sys, os, time AGING = 172800 # Default 2 days def lister(dummy, dirName, filesInDir): print '[' + dirName + ']' for fname in filesInDir: path = os.path.join(dirName, fname) if not os.path.isdir(path): print path, time.ctime(os.stat(path).st_mtime), fileage(path), prune(path) purge(path) def fileage(file): curtime = time.time() modtime = os.stat(file).st_mtime age = curtime - modtime return age def prune(file): if fileage(file) > AGING: return ("T") else: return ("File should not be deleted") def purge(file): if prune(file) == "T": os.remove(file) if __name__ == '__main__': os.path.walk(sys.argv[1], lister, None) --Adam Getchell
Re: Shell tool
I only skimmed you message, but I couldn't see os.removedirs in it: >>> help('os.removedirs')Help on function removedirs: removedirs(name) removedirs(path) -> None Super-rmdir; remove a leaf directory and empty all intermediate ones. Works like rmdir except that, if the leaf directory is successfully removed, directories corresponding to rightmost path segments will be pruned way until either the whole path is consumed or an error occurs. Errors during this latter phase are ignored -- they generally mean that a directory was not empty. >>> Hope that helps. GBU Matthew Sherborne
Re: Shell tool
Adam, Richard, Microsoft seems to have changed the email standards again; do you think you could send in plain text? Thanks, I appreciate it. (yes, my email client reads html, but it can't cope with this.) > "AG" == Adam Getchell <[EMAIL PROTECTED]> writes: AG> AG> AG> AG> AG> AG> AG> AG> AG> AG> AG> Hello all, AG> AG> I’ve just started trying to use Python to automate AG> Windows system administration. I wrote my first (long-winded, amateurish) AG> shell-tool to os.path.walk through a directory structure and delete files older AG> than 2 days. (Keeps users from cluttering some temporary storage space). It AG> works fine, except that it does not delete the empty directories. Any hints? AG> AG> """Delete files older than AGING constant AG> using os.path.walk""" AG> import sys, os, time AG> AG> AGING = 172800 # Default 2 days AG> AG> def lister(dummy, dirName, filesInDir): AG> print '[' + dirName + ']' AG> for fname in filesInDir: AG> path = AG> os.path.join(dirName, fname) AG> if not AG> os.path.isdir(path): AG> AG> print path, time.ctime(os.stat(path).st_mtime), fileage(path), prune(path) AG> AG> purge(path) AG> AG> def fileage(file): AG> curtime = time.time() AG> modtime = os.stat(file).st_mtime AG> age = curtime - modtime AG> return age AG> AG> def prune(file): AG> if fileage(file) > AGING: AG> return AG> ("T") AG> else: AG> return AG> ("File should not be deleted") AG> AG> def purge(file): AG> if prune(file) == "T": AG> AG> os.remove(file) AG> AG> AG> if __name__ == '__main__': AG> os.path.walk(sys.argv[1], lister, None) AG> AG> --Adam Getchell AG> AG> AG> AG> -- Patricia J. Hawkins Hawkins Internet Applications, LLC ___ ActivePython mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython
Shell tool
Sorry about the HTML mail -- here it is plain text. If I understand the suggestions, to do this properly I should probably be doing a Depth-First Search. Well, interestingly I need to do just this for another tool which constructs a dependency tree based upon file dependencies for a particular code. I think the webchecker tool does something similar. Before I try to write my own, I'd like to enquire about Python's "batteries" -- is there a good built-in data structure to construct an adjacency list of nodes that could be files or data sources? Here's the other tool. It sounds like I might get away with adding to the bottom of lister: if os.path.isdir(path).isempty(): purge(path) Except I'd have to loop through again ...? Thanks for all the help! """Delete files older than AGING constant using os.path.walk""" import sys, os, time AGING = 172800 # Default 2 days def lister(dummy, dirName, filesInDir): print '[' + dirName + ']' for fname in filesInDir: path = os.path.join(dirName, fname) if not os.path.isdir(path): print path, time.ctime(os.stat(path).st_mtime), fileage(path), prune(path) purge(path) def fileage(file): curtime = time.time() modtime = os.stat(file).st_mtime age = curtime - modtime return age def prune(file): if fileage(file) > AGING: return ("T") else: return ("File should not be deleted") def purge(file): if prune(file) == "T": os.remove(file) if __name__ == '__main__': os.path.walk(sys.argv[1], lister, None) --Adam Getchell ___ ActivePython mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython
RE: Shell tool
Why not use Matthew's suggestion of os.removedirs() - sounds like its all that you need. Its not as much fun, but I think that single call will do what your entire script is intending to do. - Rick -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Adam Getchell Sent: Sunday, May 12, 2002 10:17 PM To: [EMAIL PROTECTED] Subject: Shell tool Sorry about the HTML mail -- here it is plain text. If I understand the suggestions, to do this properly I should probably be doing a Depth-First Search. Well, interestingly I need to do just this for another tool which constructs a dependency tree based upon file dependencies for a particular code. I think the webchecker tool does something similar. Before I try to write my own, I'd like to enquire about Python's "batteries" -- is there a good built-in data structure to construct an adjacency list of nodes that could be files or data sources? Here's the other tool. It sounds like I might get away with adding to the bottom of lister: if os.path.isdir(path).isempty(): purge(path) Except I'd have to loop through again ...? Thanks for all the help! """Delete files older than AGING constant using os.path.walk""" import sys, os, time AGING = 172800 # Default 2 days def lister(dummy, dirName, filesInDir): print '[' + dirName + ']' for fname in filesInDir: path = os.path.join(dirName, fname) if not os.path.isdir(path): print path, time.ctime(os.stat(path).st_mtime), fileage(path), prune(path) purge(path) def fileage(file): curtime = time.time() modtime = os.stat(file).st_mtime age = curtime - modtime return age def prune(file): if fileage(file) > AGING: return ("T") else: return ("File should not be deleted") def purge(file): if prune(file) == "T": os.remove(file) if __name__ == '__main__': os.path.walk(sys.argv[1], lister, None) --Adam Getchell ___ ActivePython mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython ___ ActivePython mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython