On Thursday, April 2, 2015 at 8:26:01 AM UTC-4, Chris Angelico wrote: > On Thu, Apr 2, 2015 at 11:02 PM, Saran A <ahlusar.ahluwa...@gmail.com> wrote: > > 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. > > > > I don't think you actually want to convert a list into a string, here. > Tell me if I'm understanding your code's intention correctly: > > > The sample code that I am trying to run is: > > > > path = "/Users/Desktop/Projects/" > > dirlist = os.listdir(path) > > before = dict([(f, None) for f in os.listdir(dirlist)]) > > Start up and get a full list of pre-existing files. > > > def main(dirlist): > > while True: > > time.sleep(10) #time between update check > > Then, every ten seconds... > > > after = dict([(f, None) for f in os.listdir(dirlist)]) > > added = [f for f in after if not f in before] > > ... get a list of files, and if there are new ones... > > > if added: > > print('Successfully added new file - ready to validate') > > if __name__ == "__main__": > > main() > > ... print out a message. > > If that's what you're trying to do, I would suggest using a directory > notification system instead. Here's one that I use on Linux: > > https://github.com/Rosuav/shed/blob/master/dirwatch.py > > Here's another one, this time built for Windows: > > https://github.com/Rosuav/shed/blob/master/senddir.py > > But even if you absolutely have to poll, like that, you'll need to > make a few code changes. The exception you're getting is symptomatic > of just one problem with the code as published. My suspicion is that > you just want to use listdir(path) rather than listdir(dirlist) - but > if you want subdirectories, then you'll need to do things a bit > differently (probably using os.walk instead). > > Also: You say you're using Python 2.7. If you have no particular > reason to use 2.7, you'll do better to jump to Python 3. Your code > will probably run identically, when it's this simple. > > ChrisA
@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). 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:]) -- https://mail.python.org/mailman/listinfo/python-list