Python3 get detailed information about the contents of the specified directory

Source: Internet
Author: User

The detail commands for getting the contents of the specified directory on different platforms vary:

Linux can be obtained by Ls-al.

Windows can be obtained by using the dir command

Here's a generic python3 script that I wrote to get directory content details:
#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportOS, time, sys fromOs.pathImportJoin, GetSizedefListdir (dir_data):" ":p Aram Dir_data: Specifies a file or folder (Builder practice) to retrieve the contents of the directory each time it returns to the directory" "List_dir=Os.listdir (dir_data) whileTrue:Try: List_obj=List_dir.pop ()yieldDir_data, List_objexceptIndexerror:returndefgetdirsize (dir):" ":p Aram dir: Specifies the size of the directory to get the directory size" "size=0 forRoot, dirs, filesinchOs.walk (dir): Size+ = SUM ([GetSize (Join (root, name)) forNameinchfiles]) returnsizedefGetFileInfo (dir_data):" ":p Aram Dir_data: Calling the Listdir function is the desired parameter to get the properties and size of each file or directory within the directory and print" "Info= {}     forRoot, objinchListdir (dir_data): Dir_obj='%s/%s'%(root, obj)ifOs.path.isfile (dir_obj):#file Processing            " "info is a dictionary format, convenient to return calls, this script is only output content, does not involve returning calls" "info['timecreated'] = Os.path.getctime (dir_obj)#Get creation Timeinfo['timemodified'] = Os.path.getatime (dir_obj)#Get access Timeinfo['Size'] = Os.path.getsize (dir_obj)/1024/1024#gets the file size, in units of M            ifinfo['Size'] >= 1024:#Convert file size to Ginfo['Size'] = info['Size']/1014Print('%-5s\t%10.2fg\t%30s\t%30s\t%-20s'%('file', info['Size'], Time.ctime (info['timecreated']), Time.ctime (info['timemodified']) (obj) )elifinfo['Size'] < 1:#file size conversion Q Kinfo['Size'] = info['Size'] * 1024Print('%-5s\t%10.2fk\t%30s\t%30s\t%-20s'%('file', info['Size'], Time.ctime (info['timecreated']), Time.ctime (info['timemodified']) (obj) )Else:                Print('%-5s\t%10.2fm\t%30s\t%30s\t%-20s'%('dir', info['Size'], Time.ctime (info['timecreated']), Time.ctime (info['timemodified']) (obj) )Else:#Directory Processinginfo['timecreated'] =os.path.getctime (dir_obj) info['timemodified'] =os.path.getatime (dir_obj) info['Size'] = Getdirsize (dir_obj)/1024/1024ifinfo['Size'] >= 1024: info['Size'] = info['Size']/1014Print('%-5s\t%10.2fg\t%30s\t%30s\t%-20s'%('file', info['Size'], Time.ctime (info['timecreated']), Time.ctime (info['timemodified']) (obj) )elifinfo['Size'] < 1: info['Size'] = info['Size'] * 1024Print('%-5s\t%10.2fk\t%30s\t%30s\t%-20s'%('file', info['Size'], Time.ctime (info['timecreated']), Time.ctime (info['timemodified']) (obj) )Else:                Print('%-5s\t%10.2fm\t%30s\t%30s\t%-20s'%('dir', info['Size'], Time.ctime (info['timecreated']), Time.ctime (info['timemodified']) (obj) )if __name__=='__main__': GetFileInfo (sys.argv[1])

Win7 Execution results:
E:\chengd_ftp_server\module>python list_dir.py./file          7.22K           Mon-22:36:24        Sat 13:03:43  __pycache__file          8.47K           Sat 10:32:11 Sat        19 00:30:22   threading_socket_server.pyfile          1.56K           Sat 09:04:21        Mon 14 23:08:05   socket_ftp_main.pyfile          3.02K           sat 14:17:31        Sat 19 14:17:31 2017  list_dir.py

Centos7 Execution Results:
[[email protected] tmp]#Ls-lahTotal 128Kdrwxrwxrwt.Root root 4.0K Jul 20 18:34. Dr-xr-xr-x. Root root 253 Jul 18 13:03 .. DRWXR-xr-x 2 root root 18 17:32 20170720drwxr-xr-x 2 root root 103 Jul 19 05:58 20170721drwxr-xr-x 2 root root 141 Jul 19 13:14 20170722drwxr-xr-x 2 root root 121 Jul 20 01:33 20170723drwxr-xr-x 2 root root for 20 04:03 20170724drwxr-xr-x 2 root root 17:26 20170801-rw-r--r--1 root root 233 Jul 19 23:33Apache.log-rw-r--r--1 root root 1.4K Jul 19 23:21APACHEN.LOGDRWXRWXRWT.2 root root 6 June 15:37. font-UNIXDRWXR-xr-x 2 root root 18 13:08HSPERFDATA_ROOTDRWXRWXRWT. 2 root root 6 June 26 15:37. ice-UNIXDRWXR-xr-x. 2 root root 27 05:27SHUTIL_FILEDRWXRWXRWT. 2 root root 6 June 26 15:37. test-UNIXDRWXRWXRWT. 2 root root 6 June 26 15:37. x11-UNIXDRWXRWXRWT. 2 root root 6 June 26 15:37. xim-Unix-RW-------1 root root 97K Jul 11:27 yum_save_tx.2017-07-18.11-27. UZI4ER.YUMTX-RW-------1 root root 15K Jul 12:53 yum_save_tx.2017-07-18.12-53. Vanjxp.yumtx[[email protected] tmp]#/root/dir.py.File 1.72K Thu 17:26:17 Thu Jul 20 17:26:19 2017 20170801file1.65K Thu 04:03:46 Thu Jul 20 07:01:24 2017 20170724file 0.23K Wed Jul23:33:55 Wed Jul 19 23:34:05 2017Apache.log File1.30K Wed 23:21:24 Wed Jul 19 23:19:02 2017Apachen.log File4.88K Thu 01:33:32 Thu Jul 20 18:17:47 2017 20170723file2.13K Wed 13:14:10 Thu Jul 20 18:17:47 2017 20170722file 0.97K Wed Jul05:58:58 Thu Jul 20 18:17:47 2017 20170721file3.17K Tue 17:32:47 Thu Jul 20 18:17:47 2017 20170720file32.00K Tue 13:08:48 Thu Jul 20 18:17:47 2017hsperfdata_root File14.76K Tue 12:53:05 Tue Jul 12:53:05 yum_save_tx.2017-07-18.12-53. Vanjxp.yumtxfile96.68K Tue 11:27:03 Tue Jul 11:27:03 yum_save_tx.2017-07-18.11-27. Uzi4er.yumtxfile 0.05K Tue Jul11:34:08 Thu Jul 20 18:17:47 2017shutil_file file 0.00K Mon June15:44:13 Thu Jul 20 18:17:47 2017. ice-UNIX File 0.00K Mon June15:44:13 Thu Jul 18:17:47. font-UNIX File 0.00K Mon June15:44:13 Thu Jul 20 18:17:47 2017. x11-UNIX File 0.00K Mon June15:37:09 Thu Jul 20 18:17:47 2017. test-UNIX File 0.00K Mon June15:37:09 Thu Jul 20 18:17:47 2017. Xim-unix

The size of the file is calculated to KB, not too precise; you are interested in being able to be accurate to byte perfect

Python3 Get more information about the contents of the specified directory

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.