Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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; 'example:': 0.03; 'root': 0.05; 'tree': 0.05; '#if': 0.07; 'indicating': 0.07; 'matches': 0.07; 'paths': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'creation,': 0.09; 'ex:': 0.09; 'exist,': 0.09; 'filename': 0.09; 'filenames': 0.09; 'filenames:': 0.09; 'function,': 0.09; 'main()': 0.09; 'root,': 0.09; 'shutil': 0.09; 'spec': 0.09; 'suffix': 0.09; 'try:': 0.09; 'subject:How': 0.10; 'def': 0.12; "'w')": 0.16; 'costs.': 0.16; 'dest)': 0.16; 'dirnames,': 0.16; 'dirpath,': 0.16; 'elsewhere,': 0.16; 'exists,': 0.16; 'file))': 0.16; 'filename,': 0.16; 'filenames.': 0.16; 'files:': 0.16; "function's": 0.16; 'implies': 0.16; 'ioerror:': 0.16; 'jumps': 0.16; 'loop.': 0.16; 'main():': 0.16; 'monitors': 0.16; 'oserror': 0.16; 'parts.': 0.16; 'subject:Problem': 0.16; 'top-level': 0.16; 'torn': 0.16; 'traverse': 0.16; 'true:': 0.16; 'uppercase': 0.16; 'files.': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; 'file,': 0.19; 'addition,': 0.20; 'written': 0.21; 'seems': 0.21; 'code,': 0.22; 'import': 0.22; 'tests': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'decorators': 0.24; 'recognize': 0.24; 'refers': 0.24; "shouldn't": 0.24; 'file.': 0.24; 'this:': 0.26; 'pass': 0.26; 'least': 0.26; '(for': 0.26; 'skip:" 20': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'ideal': 0.29; "doesn't": 0.30; 'moved': 0.30; 'relative': 0.30; 'said,': 0.30; 'code': 0.31; 'bunch': 0.31; 'directory,': 0.31; 'dropped': 0.31; 'stated.': 0.31; 'file': 0.32; 'probably': 0.32; 'skip:m 30': 0.32; 'stuff': 0.32; 'another': 0.32; 'text': 0.33; 'says': 0.33; 'used,': 0.33; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'module.': 0.36; 'processed': 0.36; 'done': 0.36; "i'll": 0.36; 'subject:?': 0.36; 'should': 0.36; 'throughout': 0.37; 'unit': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'problems': 0.38; 'convention': 0.38; 'process,': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'functional': 0.39; 'moving': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'break': 0.61; 'length': 0.61; 'skip:t 30': 0.61; 'till': 0.61; 'completed': 0.61; 'march': 0.61; 'simply': 0.61; "you're": 0.61; "you've": 0.63; 'name': 0.63; 'more': 0.64; 'total': 0.65; 'different': 0.65; 'charset:windows-1252': 0.65; 'within': 0.65; 'between': 0.67; 'bottom': 0.67; 'received:74.208': 0.68; 'containing': 0.69; 'records': 0.73; 'carefully': 0.74; 'day': 0.76; '100': 0.79; 'subject:this': 0.83; '2015': 0.84; 'fragments': 0.84; 'subject:Best': 0.91; 'besides,': 0.93; 'dirty': 0.93 Date: Sun, 29 Mar 2015 09:27:33 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Addendum to Strategy/ Advice for How to Best Attack this Problem? References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:1JPGRzAH/GPaHp9CIJ7h1IaQupVIDezPc6FFs6p/SylTBDvM4Zc OlFLmxCBjHemUWcu/GSCFrSFcpAta8zf0jgQdaZLXC1euTtK6X/faSuctZyzkjjqyoG+0Xi lCHtm/h+m/0B6wXZIZihXVA9lHJFaPmh1KBb/2uyG5pGqXPCAGuo2/MuQ7UU36KggWDcbcL XyLUR2PhxpXvaxMm5+ANg== X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 140 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427635674 news.xs4all.nl 2935 [2001:888:2000:d::a6]:48548 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88269 On 03/29/2015 07:37 AM, Saran Ahluwalia wrote: > On Sunday, March 29, 2015 at 7:33:04 AM UTC-4, Saran Ahluwalia wrote: >> Below are the function's requirements. I am torn between using the OS module or some other quick and dirty module. In addition, my ideal assumption that this could be cross-platform. "Records" refers to contents in a file. What are some suggestions from the Pythonistas? >> >> * Monitors a folder for files that are dropped throughout the day >> >> * When a file is dropped in the folder the program should scan the file >> >> o IF all the records in the file have the same length >> >> o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records processed >> >> o IF the file is empty OR the records are not all of the same length >> >> o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). > > Below are some functions that I have been playing around with. I am not sure how to create a functional program from each of these constituent parts. I could use decorators or simply pass a function within another function. Your problem isn't complicated enough to either need function objects or decorators. You might want to write a generator function, but even that seems overkill for the problem as stated. Just write the code, top-down, with dummy bodies containing stub code. Then fill it in from bottom up, with unit tests for each completed function. More complex problems can justify a different approach, but you don't need to use every trick in the arsenal. > > [code] > import time > import fnmatch > import os > import shutil > If you have code fragments that aren't going to be used, don't write them as top-level code. Either move them to another file, or at least enclose them in a function with a name like dummy_do_not_use() My own convention for that is to suffix the function name with a bunch of uppercase ZZZ's That way the name jumps out at me so I'll recognize it, and I can be sure I'll never actually call it. > > #If you want to write to a file, and if it doesn't exist, do this: > > if not os.path.exists(filepath): > f = open(filepath, 'w') > > #If you want to read a file, and if it exists, do the following: > > try: > f = open(filepath) > except IOError: > print 'I will be moving this to the ' > > > #Changing a directory to "/home/newdir" > os.chdir("/home/newdir") As Peter said, chdir can be very troublesome. Avoid at almost all costs. As you've done elsewhere, use os.path.join() to combine directory paths with relative filenames. > > def move(src, dest): > shutil.move(src, dest) > > def fileinfo(file): > filename = os.path.basename(file) > rootdir = os.path.dirname(file) > lastmod = time.ctime(os.path.getmtime(file)) > creation = time.ctime(os.path.getctime(file)) > filesize = os.path.getsize(file) > > print "%s**\t%s\t%s\t%s\t%s" % (rootdir, filename, lastmod, creation, filesize) > > searchdir = r'D:\Your\Directory\Root' > matches = [] > > def search > for root, dirnames, filenames in os.walk(searchdir): Why are you using a directory tree when your "spec" said the files would be in a specific directory? > ## for filename in fnmatch.filter(filenames, '*.c'): > for filename in filenames: > ## matches.append(os.path.join(root, filename)) > ##print matches > fileinfo(os.path.join(root, filename)) > > > def get_files(src_dir): > # traverse root directory, and list directories as dirs and files as files > for root, dirs, files in os.walk(src_dir): > path = root.split('/') > for file in files: > process(os.path.join(root, file)) > os.remove(os.path.join(root, file)) Probably you shouldn't have os.remove in the code till the stuff around it has been carefully tested. Besides, nothing in the spec says you're going to remove any files. > > def del_dirs(src_dir): > for dirpath, _, _ in os.walk(src_dir, topdown=False): # Listing the files > if dirpath == src_dir: > break > try: > os.rmdir(dirpath) > except OSError as ex: > print(ex) > > > def main(): > get_files(src_dir) > del_dirs(src_dir) > Your description says "monitor". That implies to me an ongoing process, or a loop. You probably want something like: def main(): while True: process_files(directory_name) sleep(10000) > > if __name__ == "__main__": > main() > > > [/code] > -- DaveA