Python Friday #120: Modify the Create Date of a File

I wanted to test a Python script that puts files into folders based on their creation time. But how can I create files in the past without using a time machine?

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Install filedate

The Python package filedate is a great little helper when you need to read or write the different date attributes of a file. You can install it with this command:

 

Read the date attributes of a file

With filedate you can read the creation date of a file, the modified date and, depending on the operating system, the last accessed date.

{‘created’: datetime.datetime(2022, 1, 1, 13, 0),
‘modified’: datetime.datetime(2022, 4, 18, 18, 56, 3, 945619),
‘accessed’: datetime.datetime(2022, 4, 18, 18, 56, 3, 945619)}

 

Change the date attributes of a file

While many Python libraries can read the date attributes of a file, filedate is the only one I know of that can change them:

{‘created’: datetime.datetime(2022, 1, 1, 13, 0),
‘modified’: datetime.datetime(2022, 1, 1, 14, 0),
‘accessed’: datetime.datetime(2022, 1, 1, 15, 0)}

I suggest you use the ISO format (year-month-day) to set the dates. Although filedate can work with other date formats, the months and days may be interpreted differently than what you expect.

Especially in tests you should be as clear as you can be and prevent misunderstandings wherever possible.

 

Conclusion

I like the clear approach of filedate to change the creation date of my test files a lot more than mocking or patching the code involved with reading files. While changing the create date should not be a regular need, it is good to know that we can do that in Python if we must.

9 thoughts on “Python Friday #120: Modify the Create Date of a File”

  1. Great article! Any idea how to change “created date” to “modified date” for a multiple files in a folder?

    Reply
  2. Thank you for sharing these articles with us.
    Unfortunately I have a problem.
    When I want to change the date of my file, the file size is 0 KB and the file is empty.

    I have used the code as stated in this article.

    Can you help me further?

    Reply
    • Here is Juergen again,
      I see my fault.
      Please ignore my last comment.

      If you want you can delete it.

      Reply
    • Hi Hussam,
      Unfortunately, that is not possible with this library. You can work with a directory and use the same methods, but it does not change the creation date of a folder (it can change the modiefied date).

      Regards,
      Johnny

      Reply
  3. Hi, thanks for the code. I’m having trouble on Ubuntu. The access date and modification date are well set but the creation date is not modified in my case.

    Reply
    • On Windows I can also set the modify date to whatever I want, but the created date is not being changed as well.

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.