Introduction and Analysis of Python file details

Source: Internet
Author: User

Python is a commonly used language in computer languages, do you know the related code and actual application, as well as the actual application and code of Python file details? The following is a detailed introduction of the article, and I hope you will gain some benefits.

Python file time

 
 
  1. time_of_last_access = os.path.getatime(myfile)  
  2. time_of_last_modification = os.path.getmtime(myfile)  
  3. size = os.path.getsize(myfile)  

The time here is in seconds and starts from January 1, January 1, 1970. To obtain the last access date in days, use the following code:

 
 
  1. import time # time.time() 

Returns the current time.

 
 
  1. age_in_days = (time.time()-time_of_last_access)/(60*60*24)  

File details

To obtain detailed information about the file, you can use the OS. stat function and other utilities in the stat module to achieve the following purpose:

 
 
  1. import stat  
  2. myfile_stat = os.stat(myfile)  
  3. size = myfile_stat[stat.ST_SIZE]  
  4. mode = myfile_stat[stat.ST_MODE]  
  5. if stat.S_ISREG(mode):  
  6. print ’%(myfile) 

Is a regular file, the size is % (size) d byte'

 
 
  1. %\  
  2. ars()  

For details about the stat module, see Python Library Reference. To test the read, write, and execute permissions of a file, you can use the OS. access function as follows:

 
 
  1. if os.access(myfile, os.W_OK):  
  2. print myfile, 

'Write authorization'

 
 
  1. if os.access(myfile, os.R_OK | os.W_OK | os.X_OK):  
  2. print myfile, 

It has read, write, and execution permissions. 'The test code like above is very useful for CGI scripts. The above content introduces the related solutions of Python file time and file 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.