[Python] Get file properties __python

Source: Internet
Author: User

Often, we need to get the attributes of a file, such as creation date, access date, modification date, size, read-only, or hidden property. It's quite handy to use Python. Here's how I get through the data:

The file attribute was obtained using the Os.stat () method:

>>> Import OS
>>> Statinfo = Os.stat (r "C:/1.txt")
>>> Statinfo
(33206, 0L, 0, 0, 0, 0, 29L, 1201865413, 1201867904, 1201865413)

Gets the creation time of the file using the Os.stat return value Statinfo three properties

St_atime (Access Time), St_mtime (modification time), St_ctime (creation time), for example, get file creation time:

>>> Statinfo.st_ctime
1201865413.8952832

Why is such a large floating-point number AH. What does this time mean.

Recent studies have learned that this is the "seconds" starting from 1970-1-1 08:00:00, which means that this time is from 1970-1-1 to 08:00:00, after 1,201,865,413,.89,528 seconds. So what time is this time?

Use the LocalTime function in the time module to know: >>> import time
>>> Time.localtime (Statinfo.st_ctime)
(2008, 2, 1, 19, 30, 13, 4, 32, 0)

See, February 1, 2008 19:30 13 seconds (2008-2-1 19:30:13), hehe

In addition, the Statinfo properties are:

St_size (file size, expressed in bytes)

These are tested on Windows XP. Please refer to the Python manual for the countless details.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.