Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88464

Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found
Date 2015-04-02 20:03 -0400
Organization IISS Elusive Unicorn
References <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <mailman.18.1427977502.12925.python-list@python.org> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.24.1428019417.12925.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, 2 Apr 2015 05:46:57 -0700 (PDT), Saran A
<ahlusar.ahluwalia@gmail.com> 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/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:02 -0700
  Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-02 23:24 +1100
    Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:46 -0700
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 00:06 +1100
        Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 06:28 -0700
          Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 00:57 +1100
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-04-02 20:03 -0400
        Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 17:14 -0700
          Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-04-03 11:33 -0400
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 11:12 +1100
  Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Peter Otten <__peter__@web.de> - 2015-04-02 14:26 +0200
    Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:51 -0700

csiph-web