One of the best features of Python is that it is platform independent. You can write code on Linux, Windows, and MacOS and it works on all three platforms with no problems…mostly.
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)
But, what if you wanted to have this date displayed as “2022-1-5” instead? In other words, a date format without the zero-padding. How would you accomplish this? A quick look at the Python strftime() documentation notes that the “%m” and “%d” format codes always yield zero-padded results.
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:
“The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation. There are also differences between platforms in handling of unsupported format specifiers.”
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:
- First, Python may have access to (but no documentation for) some underlying features in the C library (like the no-zero-padding modifiers)—you may want or need to use these. It can be worth getting to know these C libraries
- Second, the underlying C libraries sometimes behave differently on different operating systems.
Anyone trying to achieve true platform independence in these areas may have to work around those differences in some clever fashion.
About the Author
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.
What can we learn about machine learning from ancient astronomy? When thinking about Machine Learning it is easy to be model-centric and get caught up…
Dear Students and Friends of Enthought, I am pleased to announce Enthought Academy—the culmination of over twenty years of teaching Scientific Python. Since our founding…
In the blog post Configuring a Neural Network Output Layer we highlighted how to correctly set up an output layer for deep learning models. Here,…
Introduction When we teach our foundational Python class, one of the things we do is make sure that our students know how to explore Python…
Introduction When I first started my machine learning journey, K-means clustering was one of the first algorithms I was introduced to – and it is…
Introduction At Enthought we teach a lot of scientists and engineers about using Python and the ecosystem of scientific Python packages for processing, analyzing, and…
Introduction If you have used TensorFlow before, you know how easy it is to create a simple neural network model using the Keras API. Just…
One of the best features of Python is that it is platform independent. You can write code on Linux, Windows, and MacOS and it works…
Introduction So, you have data and want to get started with machine learning. You’ve heard that machine learning will help you make sense of that…
Sorting Out .sort() and sorted() Sometimes sorting a Python list can make it mysteriously disappear. This happens even to experienced Python programmers who use .sort()…
Join Our Mailing List!
Sign up below to receive email updates including the latest news, insights, and case studies from our team.