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
- time_of_last_access = os.path.getatime(myfile)
- time_of_last_modification = os.path.getmtime(myfile)
- 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:
- import time # time.time()
Returns the current time.
- 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:
- import stat
- myfile_stat = os.stat(myfile)
- size = myfile_stat[stat.ST_SIZE]
- mode = myfile_stat[stat.ST_MODE]
- if stat.S_ISREG(mode):
- print ’%(myfile)
Is a regular file, the size is % (size) d byte'
- %\
- 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:
- if os.access(myfile, os.W_OK):
- print myfile,
'Write authorization'
- if os.access(myfile, os.R_OK | os.W_OK | os.X_OK):
- 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.