Strategy/ Advice for How to Best Attack this Problem?
davea at davea.name
Thu Apr 2 23:10:46 CEST 2015
On 04/02/2015 09:06 AM, Saran A wrote:
> Thanks for your help on this homework assignment. I started from scratch last night. I have added some comments that will perhaps help clarify my intentions and my thought process. Thanks again.
> from __future__ import print_function
I'll just randomly comment on some things I see here. You've started
several threads, on two different forums, so it's impractical to figure
out what's really up.
<snip some code I'm not commenting on>
> #Helper Functions for the Success and Failure Folder Outcomes, respectively
> def file_len(filename):
This is an indentation error, as you forgot to start at the left margin
> with open(filename) as f:
> for i, l in enumerate(f):
> return i + 1
> def copy_and_move_File(src, dest):
> shutil.rename(src, dest)
Is there a reason you don't use the move function? rename won't work if
the two directories aren't on the same file system.
> # 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)
> # Call main(), with a loop that calls # validate_files(), with a sleep after each pass. Before, my present #code was assuming all filenames come directly from the commandline. There was no actual searching #of a directory.
> # I am assuming that this is appropriate since I moved the earlier versions of the files.
> # I let the directory name be the argument to main, and let main do a dirlist each time through the loop,
> # and pass the corresponding list to validate_files.
> path = "/some/sample/path/"
> dirlist = os.listdir(path)
> before = dict([(f, None) for f in dirlist)
> #####Syntax Error? before = dict([(f, None) for f in dirlist)
> SyntaxError: invalid syntax
Look at the line in question. There's an unmatched set of brackets. Not
that it matters, since you don't need these 2 lines for anything. See
my comments on some other forum.
> def main(dirlist):
bad name for a directory path variable.
> while True:
> time.sleep(10) #time between update check
Somewhere inside this loop, you want to obtain a list of files in the
specified directory. And you want to do something with that list. You
don't have to worry about what the files were last time, because
presumably those are gone. Unless in an unwritten part of the spec,
you're supposed to abort if any filename is repeated over time.
> after = dict([(f, None) for f in dirlist)
> added = [f for f in after if not f in before]
> if added:
> print('Sucessfully added new file - ready to validate')
> ####add return statement here to pass to validate_files
> if __name__ == "__main__":
You'll need an argument to call main()
> #check for record time and record length - logic to be written to either pass to Failure or Success folder respectively
> def validate_files():
Where are all the parameters to this function?
> creation = time.ctime(os.path.getctime(added))
> lastmod = time.ctime(os.path.getmtime(added))
> #Potential Additions/Substitutions - what are the implications/consequences for this
> def move_to_failure_folder_and_return_error_file():
> copy_and_move_File(filename, 'Failure')
> logging.error("Either this file is empty or there are no lines")
> def move_to_success_folder_and_read(f):
> copy_and_move_File(filename, 'Success')
> print("Success", f)
> return file_len()
> #This simply checks the file information by name------> is this needed anymore?
> def fileinfo(file):
> filename = os.path.basename(f)
> rootdir = os.path.dirname(f)
> filesize = os.path.getsize(f)
> return filename, rootdir, filesize
> if __name__ == '__main__':
> import sys
> # -- end of file
More information about the Python-list