Python get file modification time




















Add a comment. Active Oldest Votes. You can access this in Python through os. This won't work on Unix, where the ctime is the last time that the file's attributes or content were changed. On Mac , as well as some other Unix-based OSes, you can use the. Putting this all together, cross-platform code should look something like this No easy way to get creation dates here, so we'll settle for when its content was last modified.

Mark Amery Mark Amery k 67 67 gold badges silver badges bronze badges. I've done my best to throw this together and spent a few hours researching in the process , and I'm sure it's at least more correct than the answers that were here previously, but this is a really hard topic and I'd appreciate any corrections, clarifications, or other input that people can offer.

Frankly, file creation time is usually fairly useless. When you open an existing file for write with mode "w" , it's not replacing it, it just opens the existing file and truncates it.

Even though the file contents are completely unrelated to whatever it had on creation, you'd still be told the file was "created" well before the current version. Conversely, editors that use atomic replace on save original file is replaced by new work-in-progress temp file would show a more recent creation date, even if you just deleted one character.

Use the modification time, don't grub for creation time. After many years, I've finally found a use for file creation time! I'm writing code to check a file naming convention in certain directories, so first of all I want to consider files that were first named after the convention was instituted. Replacing the entire contents mtime is irrelevant: if it was already there then it's grandfathered in. Hi Mark. I propose a simplification.

On Linux, returning stat. What do you think? Cheers — oHo. Unless you've manually messed with the values in your inode, ctime should always be equal to or later than mtime , because an mtime change causes a ctime change because the mtime itself is considered "metadata".

See stackoverflow. Show 4 more comments. Bryan Oakley Bryan Oakley k 43 43 gold badges silver badges bronze badges. I wonder how many people have skimmed this post over the past three years and gone on to write buggy code. Keep in mind the first example gives you a string, not a datetime or number.

Treating ctime as "created" time thus makes no sense at all. Your first option returns the same results for both file creation and last modification! Converting the time in seconds to a timestamp. Using the timestamp string to create a. Transforming the time object to a timestamp. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. The meaning differs depending on the OS, so be especially careful about the creation time. You can get file metadata, such as timestamps, as attributes of the os. You can get the os. Path object. You can also use the os. The argument can be a path string or a pathlib. Path object Python 3.

The meaning of each is described later. By changing format string in time. Instead of time. Then we can call time. To get the last modification time in UTC timezone use datetime. Your email address will not be published.



0コメント

  • 1000 / 1000