Fixing Python OSError Errno 22 Invalid argument

Fixing Python OSError: [Errno 22] Invalid argument is an error which occurs when you try to construct datetimes by using fromtimestamp.

In this article I am going to explain what happens when you get this error and how you can solve it with a main solution, we will also explore other solutions which can possibly solve the issue.

Explaining the Error : OSError Errno 22 Invalid argument

This error occurs when you try to construct datetimes by using fromtimestamp.

The error message should look like the error in the example bellow, make sure you have the same error message in order to avoid confusion.

Fixing Python OSError Errno 22 Invalid argument-problem-banner

Bellow I make my best attempt at solving the error and present multiple possible solutions.

Solution 1 : use datetime with timedelta

Since in windows the timestamp number is multiplied by a 1000 this will cause the number to become bigger than the range of values supported which are usually the equivalent of years between 1970 and 2038.

If you are on windows this code will solve your problem

Fixing Python OSError Errno 22 Invalid argument-problem-banner-1

This solution should be enough to remove the error, try the fix bellow if this one did not work.

Solution 2 : divide the timestamp value by a 1000.

The problem is that the timestamp expected from fromtimestamp is in seconds but in windows the timestamp number is multiplied by a 1000.

The fix is simple, divide the timestamp value by a 1000 and the issue should be resolved.

I hope this easy fix suits you. Thank you for reading.

Summing-up

This is the end of the article, the error Python OSError: [Errno 22] Invalid argument could be confusing but with a little bit of investigating the error could be solved, thanks for reading our post and good luck with the scripts to come.

If you want to support us consider donating to our Kofi account using the red button on top of this page. Keep coding, keep learning Python and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/