Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.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; 'example:': 0.03; 'handler': 0.05; 'that?': 0.05; 'debug': 0.07; 'indicating': 0.07; 'sys': 0.07; '__name__': 0.09; 'calls.': 0.09; 'creation,': 0.09; 'file)': 0.09; 'filename': 0.09; 'os.path': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'record.': 0.09; 'subject:string': 0.09; 'try:': 0.09; 'def': 0.12; 'assume': 0.14; 'suggest': 0.14; "%s'": 0.16; "'__main__':": 0.16; 'dest': 0.16; 'dest)': 0.16; 'filename,': 0.16; 'finds': 0.16; 'garbage': 0.16; 'ioerror': 0.16; 'length)': 0.16; 'length,': 0.16; 'logger': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'src': 0.16; 'subject:Programming': 0.16; 'subject:Unicode': 0.16; '(you': 0.16; 'folder': 0.16; 'differ': 0.19; 'subject:need': 0.19; 'thu,': 0.19; 'written': 0.21; 'import': 0.22; 'error': 0.23; '(by': 0.24; 'byte': 0.24; 'prototype': 0.24; 'skip:% 10': 0.24; 'skip:l 30': 0.24; 'unicode': 0.24; 'url:home': 0.24; 'file.': 0.24; "haven't": 0.24; 'source': 0.25; 'handling': 0.26; 'pass': 0.26; 'least': 0.26; '(for': 0.26; 'defined': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'character': 0.29; "doesn't": 0.30; 'errors': 0.30; 'moved': 0.30; 'subject:list': 0.30; 'code': 0.31; 'lines': 0.31; 'bunch': 0.31; 'dropped': 0.31; 'indentation': 0.31; 'file': 0.32; 'skip:m 30': 0.32; 'text': 0.33; 'running': 0.33; 'checking': 0.33; 'actual': 0.34; 'updated': 0.34; 'maybe': 0.34; 'could': 0.34; 'info': 0.35; 'except': 0.35; 'requirement': 0.35; 'but': 0.35; 'really': 0.36; 'processed': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'subject:New': 0.37; 'throughout': 0.37; 'wrong': 0.37; 'level': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'checks': 0.38; 'same.': 0.38; 'skip:m 40': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'most': 0.60; 'length': 0.61; 'skip:t 30': 0.61; 'new': 0.61; 'success': 0.61; 'simply': 0.61; 'first': 0.61; 'information': 0.63; 'name': 0.63; 'total': 0.65; 'determine': 0.67; 'day': 0.76; '100': 0.79; '2015': 0.84; 'characters,': 0.84; 'here)': 0.84; 'respectively': 0.84; 'destination': 0.91; 'received:108': 0.93; 'subject:found': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Date: Thu, 02 Apr 2015 20:03:23 -0400 Organization: IISS Elusive Unicorn References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-79-223-142.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 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: 137 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428019417 news.xs4all.nl 2964 [2001:888:2000:d::a6]:57174 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88464 On Thu, 2 Apr 2015 05:46:57 -0700 (PDT), Saran A declaimed the following: > >@ChrisA - this is a smaller function that will take the most updated file. My intention is the following: > >* Monitor a folder for files that are dropped throughout the day > I would suggest that your first prototype is to be a program that contains a function whose only purpose is to report on the files it finds -- forget about all the processing/moving of the files until you can successfully loop around the work of fetching the directory and handling the file names found (by maybe printing the names of the ones determined to be new since last fetch). >* When a file is dropped in the folder the program should scan the file > >o IF all the contents in the file have the same length (let's assume line length) > >o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records/lines/words processed > >o IF the file is empty OR the contents 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). > You still haven't defined how you determine the "correct length" of the record. What if the first line is 79 characters, and all the others are 80 characters? Do you report ALL lines EXCEPT the first as being the wrong length, when really it is the first line that is wrong? Also, if the files are Unicode (UTF-8, in particular) -- the byte length of a line could differ but the character length could be the same. >Here is the code I have written: > >import os >import time >import glob >import sys > >def initialize_logger(output_dir): > logger = logging.getLogger() > logger.setLevel(logging.DEBUG) > > # create console handler and set level to info > handler = logging.StreamHandler() > handler.setLevel(logging.INFO) > formatter = logging.Formatter("%(levelname)s - %(message)s") > handler.setFormatter(formatter) > logger.addHandler(handler) > > # create error file handler and set level to error > handler = logging.FileHandler(os.path.join(output_dir, "error.log"),"w", encoding=None, delay="true") > handler.setLevel(logging.ERROR) > formatter = logging.Formatter("%(levelname)s - %(message)s") > handler.setFormatter(formatter) > logger.addHandler(handler) > > # create debug file handler and set level to debug > handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"w") > handler.setLevel(logging.DEBUG) > formatter = logging.Formatter("%(levelname)s - %(message)s") > handler.setFormatter(formatter) > logger.addHandler(handler) > >#Helper Functions for the Success and Failure Folder Outcomes, respectively > >#checks the length of the file > def file_len(filename > with open(filename) as f: > for i, l in enumerate(f): > pass > return i + 1 > >#copies file to new destination > > def copyFile(src, dest): > try: > shutil.copy(src, dest) > # eg. src and dest are the same file > except shutil.Error as e: > print('Error: %s' % e) > # eg. source or destination doesn't exist > except IOError as e: > print('Error: %s' % e.strerror) > >#Failure Folder > >def move_to_failure_folder_and_return_error_file(): > os.mkdir('Failure') > copyFile(filename, 'Failure') > initialize_logger('rootdir/Failure') > logging.error("Either this file is empty or the lines") > ># Success Folder Requirement > >def move_to_success_folder_and_read(file): > os.mkdir('Success') > copyFile(filename, 'Success') > print("Success", file) > return file_len() > > >#This simply checks the file information by name > >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) > return filename, rootdir, lastmod, creation, filesize > >if __name__ == '__main__': > import sys > validate_files(sys.argv[1:]) Yeesh... Did you even try running that? validate_files is not defined file_len is at the wrong indentation is syntactically garbage is a big time-waste (you read the file just to enumerate the number of lines? Why didn't you count the lines while checking the line lengths) copyFile is at the wrong indentation (after a bunch of word_word, why camelCase here) Correct all the edit errors and copy/paste the actual file that at least attempts to run. You might also want to look at os.stat, rather than using three os.path calls. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/