Nice

Yesterday I had one problem with folders created by my photo transfer script. The problem was with the following line:

yyyymmdd_dir = os.path.join(dst_dir, str(yyyy) + "-" + mm)

I had to fill a zero to the month number if it was less than ten, before creating the folder name.
I wrote this:

mm == str(mm)
if len(mm) == 1:
mm = "0" + mm
yyyymmdd_dir = os.path.join(dst_dir, str(yyyy) + "-" + mm)

Then I searched what’s the nicest way to zero fill a string? And I found this question on StackOverflow.
My code then became:

yyyymm_dir = os.path.join(dst_dir, str(yyyy) + "-" + str(mm).zfill(2))

So “nicest” might mean the shortest easily readable code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s