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

Saran A ahlusar.ahluwalia at gmail.com
Thu Apr 2 14:51:50 CEST 2015


On Thursday, April 2, 2015 at 8:26:51 AM UTC-4, Peter Otten wrote:
> Saran A wrote:
> 
> > Good Morning:
> > 
> > I understand this error message when I run this code. However, I am
> > curious to know what the most pythonic way is to convert  the list to a
> > string? I use Python 2.7.
> > 
> > "Traceback (most recent call last):
> > before = dict([(f, None) for f in os.listdir(dirlist)])
> > TypeError: coercing to Unicode: need string or buffer, list found"
> > 
> > 
> > The sample code that I am trying to run is:
> > 
> > path = "/Users/Desktop/Projects/"
> > dirlist = os.listdir(path)
> 
> At this point dirlist is a list of names of the files and directories in 
> 
> "/Users/Desktop/Projects/"
> 
> Assuming that the Projects folder contains the subfolders or files
> /Users/Desktop/Projects/foo, /Users/Desktop/Projects/bar and 
> /Users/Desktop/Projects/baz dirlist looks like this:
> 
> ["foo", "bar", "baz"]
> 
> It makes no sense to pass this list to os.listdir() as you do below:
> 
> > before = dict([(f, None) for f in os.listdir(dirlist)])
> 
> Forget about the other details in the error message; the actual problem is 
> the "list found" part.
> 
> Now what would be a possible fix? Sorry, I have no idea what your intention 
> is. Again, you don't need to convert your list to string, you need to decide 
> what directory you want to pass to listdir(). If you have multiple such 
> directories you need to invoke listdir() multiple times with a single 
> directory, typically in a loop.
> 
> Bonus info:
> 
> >     while True:
> >         time.sleep(10) #time between update check
>  
> This loop will never terminate.

@Peter I understand that the intention of this program is to not terminate. Here is what I have written so far:

I thought I would run this by you, since you offer such valuable feedback, in the past. Just a quick rundown on what I want my program to do:

* 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).

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)


def main(dirslist):     
    while True:
        for file in os.listdir(dirslist) :
        	return validate_files(file)
        	time.sleep(5)

if __name__ == "__main__": 
    main() 


#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:])



I am trying to specifically address the fact that the program does not:

The present code does not move any files to success or failure directories (I have added functions at the end that could serve to address this requirement)

The present code doesn't calculate or write to a text file. 

The present code runs once through the names, and terminates.  It doesn't "monitor" anything  - I think that I have added the correct while loop to address this

The present code doesn't check for zero-length files 

-Saran-




More information about the Python-list mailing list