Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!feeder3.cambriumusenet.nl!feed.tweaknews.nl!193.201.147.92.MISMATCH!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parser': 0.05; 'pep': 0.07; 'typed': 0.07; '#print': 0.09; 'arguments.': 0.09; 'eclipse': 0.09; 'filename': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'url:dev': 0.09; 'url:peps': 0.09; 'value:': 0.09; 'def': 0.12; 'written': 0.14; 'wrote:': 0.14; '3.2.': 0.16; 'false:': 0.16; 'mercurial.': 0.16; 'naming': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'simplified': 0.16; 'subject:Review': 0.16; 'url:pep-0008': 0.16; 'argument': 0.16; 'seconds': 0.16; 'performing': 0.21; 'stuff': 0.22; 'converts': 0.23; 'subject:Code': 0.23; "what's": 0.23; 'code': 0.24; 'function': 0.25; 'pass': 0.27; 'script': 0.27; 'wondering': 0.28; 'skip:p 30': 0.28; 'etc.)': 0.29; 'version': 0.29; 'skip:( 20': 0.30; 'second': 0.30; 'etc.,': 0.30; 'from:addr:web.de': 0.30; 'pasted': 0.30; 'it.': 0.31; 'ago': 0.31; 'separate': 0.31; 'print': 0.31; 'this.': 0.31; 'header:X -Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; '...': 0.34; 'normally': 0.34; 'there': 0.35; 'conventions': 0.35; 'deleted.': 0.35; 'explicit': 0.35; 'preserve': 0.35; 'tasks.': 0.35; 'using': 0.35; 'actual': 0.36; 'systems,': 0.36; 'idea': 0.36; 'skip:o 20': 0.37; 'ways': 0.37; 'put': 0.37; 'two': 0.37; 'url:python': 0.38; 'received:org': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'should': 0.39; 'empty': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'delete': 0.40; 'designed': 0.65; 'below.': 0.65; 'imagine': 0.72; 'enhancement': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Code Review Date: Wed, 25 May 2011 09:22:31 +0200 Organization: None References: <37ba7b40-3663-4094-b507-696fc598bf48@l26g2000yqm.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849b93.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 87 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306308167 news.xs4all.nl 49048 [::ffff:82.94.164.166]:39587 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6198 ad wrote: > Please review the code pasted below. I am wondering what other ways > there are of performing the same tasks. This was typed using version > 3.2. The script is designed to clean up a directory (FTP, Logs, etc.) > Basically you pass two arguments. The first argument is an number of > days old to delete. The second argument is the directory where the > files and folders should be deleted. I imagine one enhancement would > be to create a function out of some of this. > CurrentTime = time.time() Read PEP 8 on naming conventions etc., see http://python.org/dev/peps/pep-0008/ > > epocDay = 86400 # seconds > > > > > > parser = argparse.ArgumentParser(description = "Delete files and What's the purpose of those many empty lines? > folders in a directory N days old", add_help=False, > prog='directorycleaner', usage='%(prog)s 7 c:\\temp') > > parser.add_argument('days', type=int, help="Numeric value: delete > files and folders older then N days") > > parser.add_argument('directory', help="delete files and folders in > this directory") > > parser.print_help() What's the idea behind add_help=False and the explicit print_help()? > dictKeys = (vars(args)) > HowManyDays = dictKeys['days'] This can be simplified to HowManyDays = args.days > if dirExists == False: print ("The directory is missing") if x == False: ... is normally written as if not x: ... > DirListing = os.listdir(WhatDirectory) > for files in DirListing: You might write this as for filename in os.listdir(WhatDirectory): ... or even for filename in os.listdir(args.directory): ... Personally I would put this stuff in a separate function like def remove_old_files_and_folders(parent_directory, age_in_seconds): ... > # time.ctime converts epoch to a normal date > > #print (time.ctime(CurrentTime)) > > # Get the date from seven days ago > > WeekOldFileDate = CurrentTime - DaysToDelete > > #print (CurrentTime) > > #print (FileCreationTime) > > #print (WeekOldFileDate) Don't let out-commented code eclipse the actual code; remove it. If you want to preserve it for eternity, have a look at version control systems, e. g. Mercurial.