[Tutor] (no subject)
Peter Otten
__peter__ at web.de
Sat Mar 23 05:16:41 EDT 2019
Matthew Herzog wrote:
> 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?
Personally I would use a try...except clause because with that you can
handle invalid dates like 99999999_etc.xls gracefully. So
ERASED = "erased_"
def strip_prefix(name):
if name.startswith(ERASED):
name = name[len(ERASED):]
return name
def extract_date(name):
datestr = strip_prefix(name)[:8]
return datetime.datetime.strptime(datestr, DATEFMT).date()
for name in ...:
try:
file_date = extract_date(name)
except ValueError:
pass
else:
print(file_date)
More information about the Tutor
mailing list