Admittedly there are some issues. Most of these are from known operating system differences when accessing system subprocesses or dealing with various local quirks of file systems and security schemes. These kinds of problems are expected. However, there are some lesser known problems that only emerge in very specific circumstances. One of these is controlling zero padding with the strftime() function in Python’s datetime module.
In most cases, zero-padding is needed and wanted. This is the case when formatting something like a date in a standard ISO 8601 calendar date format:
>>> from datetime import datetime
>>> d = datetime(2022, 1, 5)
>>> print(d.strftime("%Y-%m-%d"))
2022-01-05
Python being Python, there is an easy solution to the immediate problem:
>>> s = d.strftime("%Y-%m-%d").replace("-0", "-")
>>> print(s)
2022-1-5
If you want a more general solution that handles various cases—like having strftime() produce the non-zero-padded results at the get go—you have to dig a little deeper. In this case, that means reading the Python strftime() documentation with care and noticing this detail:
This is where Python’s platform independence ends: at the C library that Python calls to perform the requested formatting service. If you look at the Windows C strftime() documentation, you will discover more options for formatting dates and times, including one that will remove the zero-padding:
>>> print(d.strftime("%Y-%#m-%#d")) # Windows
2022-1-5
If you try the same thing on MacOS or Linux you get:
>>> print(d.strftime("%Y-%#m-%#d")) # MacOS or Linux
2022-#m-#d
Reading the MacOS or Linux strftime() documentation reveals that ‘#’ is a valid formatting code modifier, but used for a different purpose on these platforms. Still you can see that it is mostly an overlapping set of formatting codes, with some differences from Windows. On these platforms, you will want to try using the dash (–) to indicate not padding a numeric result string.
>>> print(d.strftime("%Y-%-m-%-d")) # MacOS or Linux
2022-1-5
If you try to run this version on Windows:
>>> print(d.strftime("%Y-%-m-%-d")) # Windows
ValueError: Invalid format string
In this case, Windows raises an error and lets you know something is wrong.
Unless you run into this particular problem while trying to support multiple operating systems, this is mostly a cautionary tale about Python’s reliance on underlying C libraries to provide some of its lowest-level services. With that, you should keep in mind two key points:
Anyone trying to achieve true platform independence in these areas may have to work around those differences in some clever fashion.
Eric Olsen holds a Ph.D. in history from the University of Pennsylvania, a M.S. in software engineering from Pennsylvania State University, and a B.A. in computer science from Utah State University. Eric spent three decades working in software development in a variety of fields, including atmospheric physics research, remote sensing and GIS, retail, and banking. In each of these fields, Eric focused on building software systems to automate and standardize the many repetitive, time-consuming, and unstable processes that he encountered.