#!/usr/bin/env Python3#-*-coding:utf-8-*-#lists the file size and creation date and file name of the current directory, equivalent to the ls-l command fromDatetimeImportdatetimeImportospwd= Os.path.abspath ('.')Print('Size last Modified Name')Print('------------------------------------------------------------') forFinchOs.listdir (PWD): F_size=os.path.getsize (f)ifF_size > 1024: K_size= Round (f_size/1024, 1) F_size= str (k_size) +'K' ifK_size > 1024: M_size= Round (k_size/1024, 1) F_size= str (m_size) +'M' ifM_size > 1024: G_size= Round (m_size/1024, 1) F_size= str (g_size) +'G' Else: F_size= str (f_size) +'B'Mtime= Datetime.fromtimestamp (Os.path.getmtime (f)). Strftime ('%y-%m-%d%h:%m') Flag='/' ifOs.path.isdir (f)Else "' " "if Os.path.isdir (f): t = '/' Else:t = """ " #print ('%10d%s%s%s '% (fsize, mtime, F, flag)) Print("{: >10}{:^20}{}{}". Format (f_size, Mtime, F, flag))
Python Learning-the standard library OS example (3)