[Tutor] (no subject)

Matthew Herzog matthew.herzog at gmail.com
Fri Mar 22 17:45:54 EDT 2019


I have a Python3 script that reads the first eight characters of every
filename in a directory in order to determine whether the file was created
before or after 180 days ago based on each file's name. The file names all
begin with YYYYMMDD or erased_YYYYMMDD_etc.xls. I can collect all these
filenames already.
I need to tell my script to ignore any filename that does not conform to
the standard eight leading numerical characters, example: 20180922 or
erased_20171207_1oIkZf.so.
Here is my code.

if name.startswith('scrubbed_'):
            fileDate = datetime.strptime(name[9:17], DATEFMT).date()
        else:
            fileDate = datetime.strptime(name[0:8], DATEFMT).date()

I need logic to prevent the script from 'choking' on files that don't fit
these patterns. The script needs to carry on with its work and forget about
non-conformant filenames. Do I need to add code that causes an exception or
just add an elif block?

Thanks.


-- 
The plural of anecdote is not "data."


More information about the Tutor mailing list