6

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?

3
  • 3
    stackoverflow.com/questions/1158076/… Commented Feb 25, 2018 at 11:54
  • 1
    Why on earth was this downvoted? @ShravanYadav: The question you linked to is just about touching a file, not about setting a timestamp. Commented Oct 30, 2018 at 14:18
  • 1
    I have not downvoted it. Why should I downvote and put an answer to it? Commented Oct 31, 2018 at 9:44

2 Answers 2

4

You can also call the touch command in python

from subprocess import call
call(["touch","-a","-m","-t","201512180130.09","file.txt"])
Sign up to request clarification or add additional context in comments.

2 Comments

sorry there is already answer to this stackoverflow.com/questions/1158076/…
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.
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.