Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the modified date/time of a file in Python? [duplicate]

Tags:

How do I get the modified date/time of a file in Python?

like image 601
Matt Avatar asked Sep 23 '08 13:09

Matt


People also ask

How do I get the date modified of a file in Python?

Using time module to get file creation & modification date or time in Python. We will use getctime() and getmtime() function, found inside the path module in the os library, for getting the creation and modification times of the file.

How can I tell what time a file was modified?

Use the cd command to access the directory containing the files you want to see the modification date. Then, use the ls command to list all files in the current directory. We recommend using the ls command below to give you the best listing of files, including the modification date.

How do I get the timestamp of a file in Python?

Use getctime() function to get a creation timepath. getmtime('file_path') function returns the creation time of a file. On the other hand, on Unix, it will not work. Instead, it will return the timestamp of the last time when the file's attributes or content were changed (most recent metadata change on Unix).

How do I find file creation and modification date time in Linux?

The easiest way to get the file creation date is with the stat command. As we can see, the creation date is shown in the “Birth” field.


2 Answers

os.path.getmtime(filepath) 

or

os.stat(filepath).st_mtime 
like image 115
Thomas Wouters Avatar answered Oct 21 '22 20:10

Thomas Wouters


Formated:

import time print time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(fname))) 
like image 41
Dingo Avatar answered Oct 21 '22 21:10

Dingo