On Mar 30, 2018 10:39, Alan Gauld via Tutor <tutor@python.org> wrote: > > On 30/03/18 03:48, Pat Martin wrote: > > > the "right" way to do it in python? > > More or less, a couple of comments below... > > > def Main(): > > Python function names begin with a lowercase letter by convention. > > > """Run if run as a program.""" > > parser = argparse.ArgumentParser() > ... > > > > > now = datetime.datetime.now() > > slug = args.title.replace(" ", "-").lower() > > > > with open("{}.md".format(slug), 'w') as f: > > f.write("Title: {}\n".format(args.title)) > > f.write("Date: {}-{}-{} {}:{}\n".format(now.year, > > now.month, > > now.day, > > now.hour, > > now.minute)) > > Formatting of dates and times is usually done using > the time.strftime function which is specifically > designed for that. It might be worth taking a peek > at the docs on that one. You can call it directly > on a datetime object - 'now' in your case: > > fmt="%Y-%m-%d %H:%M\n" > f.write(now.strftime(fmt))
Lately I've been using format(), which uses __format__, because I find it slightly more readable: format(datetime.now(), "%Y-%m-%d %H:%M") _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor