5

In Linux, I can change the creation date of a file using touch:

touch -a -m -t 201512180130.09 file.txt

In Python, I can use the touch command to change the creation date, but I don't see any arguments to set it to a different date than the current one.

from pathlib import Path
Path('path/to/file.txt').touch()

How can I set the date in Python?

SPRBRN
  • 2,406
  • 4
  • 35
  • 48

2 Answers2

4

You can also call the touch command in python

from subprocess import call
call(["touch","-a","-m","-t","201512180130.09","file.txt"])
Shravan Yadav
  • 1,297
  • 1
  • 14
  • 26
  • 1
    sorry there is already answer to this https://stackoverflow.com/questions/1158076/implement-touch-using-python – Shravan Yadav Feb 25 '18 at 11:54
  • 1
    This is not a cross-platform solution, and you've linked to a question, not an answer. The question isn't even the same; it's just about touching, not changing a file's timestamp. – Michael Scheper Oct 30 '18 at 14:20
4

You can use os.utime to set the access and modified times of a file.

stenci
  • 8,290
  • 14
  • 64
  • 104