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; 'indicating': 0.07; 'linux,': 0.07; 'sys': 0.07; '__name__': 0.09; 'callback': 0.09; 'subject:string': 0.09; 'try:': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'assume': 0.14; "%s'": 0.16; "'__main__':": 0.16; 'dest': 0.16; 'dest)': 0.16; 'does,': 0.16; 'flush': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ioerror': 0.16; 'length)': 0.16; 'logger': 0.16; 'src': 0.16; 'subject: \n ': 0.16; 'subject:Programming': 0.16; 'subject:Unicode': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'subject:need': 0.19; 'thu,': 0.19; 'written': 0.21; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'instead.': 0.24; 'skip': 0.24; 'fairly': 0.24; 'file.': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'source': 0.25; 'equivalent': 0.26; 'pass': 0.26; '(for': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; "doesn't": 0.30; 'moved': 0.30; 'then.': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'included': 0.31; 'code': 0.31; 'getting': 0.31; 'that.': 0.31; 'dropped': 0.31; 'exceptions': 0.31; 'file': 0.32; 'text': 0.33; 'up.': 0.33; 'updated': 0.34; 'could': 0.34; 'common': 0.35; 'except': 0.35; 'received:google.com': 0.35; 'processed': 0.36; 'doing': 0.36; 'should': 0.36; 'subject:New': 0.37; 'throughout': 0.37; 'skip:m 40': 0.38; 'files': 0.38; 'pm,': 0.38; 'moving': 0.39; 'called': 0.40; 'most': 0.60; 'length': 0.61; 'new': 0.61; 'world.': 0.61; 'simple': 0.61; "you're": 0.61; 'total': 0.65; 'here': 0.66; 'services.': 0.70; 'day': 0.76; '100': 0.79; '2015': 0.84; 'destination': 0.91; 'to:none': 0.92; 'subject:found': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=pNghDl6EVaZSNZUeeXI4zbrHfNJqZGwY6jsg0+u+rsw=; b=MKab5R2k+Yf6xwFY+a5suiSBUn4tRO8UGoCrNVuWA8kWCaKGvY1oPpHacnJG1se1m2 AMe2FZqXAhE8apHqWAnVFgaHmh3FKp4YJxLLzuUo5Cthe8gi//P4TSNOlRsyeLafSMh2 NXRtJ7sHrqXztlFEWN+5vWc8/ACui0LOHdih96MzXxZ3FcU0EKyNW/oy3aKGGXZjpTl/ AAYEP59Vps0/KaPw4x0uWhk+pSF9HGc4PvOD9P9Zx5V1YUrj56Z0kVUjtOKXy3fz2Bp1 JFekfPoe3hHKpux6Fb2xiBGovs0YXx/dGnHJ9RSUYO/o+0nO6vrcBf0rctuJi4CCdOBY 5FfA== MIME-Version: 1.0 X-Received: by 10.107.16.87 with SMTP id y84mr11273468ioi.53.1427979991758; Thu, 02 Apr 2015 06:06:31 -0700 (PDT) In-Reply-To: <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> Date: Fri, 3 Apr 2015 00:06:31 +1100 Subject: Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427979994 news.xs4all.nl 2927 [2001:888:2000:d::a6]:39042 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88447 On Thu, Apr 2, 2015 at 11:46 PM, Saran A wrote: > @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 > > * 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). > Sounds like a perfect job for inotify, then. Your function will be called whenever there's a new file. > Here is the code I have written: > > def initialize_logger(output_dir): > logger = logging.getLogger() > ... > def file_len(filename > with open(filename) as f: > for i, l in enumerate(f): > pass > return i + 1 These functions are all getting defined inside your initialize_logger() function. I suspect you want them to be flush left instead. > 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) Recommendation: Skip the try/except, and just let exceptions bubble up. Don't just print out messages and keep going. > 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") This doesn't move the file, it copies it. Is that your intention? Moving a file is pretty easy. Just use os.rename(). > if __name__ == '__main__': > import sys > validate_files(sys.argv[1:]) I've no idea what validate_files() does, as you haven't included that. I think you could code this fairly efficiently as a simple callback off pyinotify, or if you're not on Linux, with one of the equivalent services. What you're doing here (watching for files, looking inside them, and moving them when done) is pretty common around the world. ChrisA