When we extract a file meta-information, we often use the size to get the meta-information, but by default it is calculated in bytes, and we need to convert to MB or GB or TB size.
therefore need to use to humansize this module, this module is not built-in system, you can query through Help () the built-in module is not this module. It needs to be imported from other places before it can be used.
Save the following file as humansize.py in the default Python directory.
1 " ""Convert file sizes to human-readable form.2 3 Available functions:4 approximate_size (Size, a_kilobyte_is_1024_bytes)5 takes a file size and returns a human-readable string6 7 Examples:8 >>> approximate_size (1024x768)9 ' 1.0 KiB 'Ten >>> approximate_size (+, False) One ' 1.0 KB ' A - " " - thesuffixes = {1000: ['KB','MB','GB','TB','PB','EB','ZB','YB'], -1024: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','Yib']} - - defApproximate_size (Size, a_kilobyte_is_1024_bytes=True): + " "' Convert a file size to human-readable form. - + Keyword arguments: A Size--File size in bytes at a_kilobyte_is_1024_bytes--if True (default), use multiples of 1024x768 - if False, use multiples of - - returns:string - - " " in ifSize <0: - RaiseValueError ('Number must be non-negative') to +multiple = 1024ifA_kilobyte_is_1024_bytesElse1000 - forSuffixinchSuffixes[multiple]: theSize/=multiple * ifSize <Multiple: $ return '{0:.1f} {1}'. Format (size, suffix)Panax Notoginseng - RaiseValueError ('Number too large') the + if __name__=='__main__': A Print(Approximate_size (1000000000000, False)) the Print(Approximate_size (1000000000000)) + - #Copyright (c), Mark Pilgrim, All rights reserved. $ # $ #redistribution and use in source and binary forms, with or without modification, - #is permitted provided that the following conditions is met: - # the #* Redistributions of source code must retain the above copyright notice, - #This list of conditions and the following disclaimer. Wuyi #* redistributions in binary form must reproduce the above copyright notice, the #This list of conditions and the following disclaimer in the documentation - #and/or other materials provided with the distribution. Wu # - #This software are provided by the COPYRIGHT holders and CONTRIBUTORS ' as is ' About #and any EXPRESS OR implied warranties, including, but not LIMITED to, the $ #implied warranties of merchantability and FITNESS for A particular PURPOSE - #Is disclaimed. In NO EVENT shall the COPYRIGHT OWNER OR CONTRIBUTORS is - #liable for any DIRECT, INDIRECT, incidental, special, exemplary, OR - #consequential damages (including, but not LIMITED to, procurement of A #substitute GOODS OR SERVICES; LOSS of Use, DATA, OR profits; OR Business + #interruption) However caused and on any theory of liability, WHETHER in the #Contract, STRICT liability, or TORT (including negligence OR OTHERWISE) - #arising in an any-out-of-the-software, even IF advised of the $ #possibility of SUCH DAMAGE.
The function of the program is to convert the file size in bytes into a more readable form, such as converting the 1024x768 to 1KB
1>>>ImportSYS2>>>ImportOS3>>> metadata = Os.stat ('Test.docx')4>>>metadata5Nt.stat_result (st_mode=33206, st_ino=0l, st_dev=0l, st_nlink=0, St_uid=0, St_gid=0, st_size=4934265l, st_atime= 1472518662L, st_mtime=1469435178l, st_ctime=1472518662l)6>>>metadata.st_size74934265L8>>>Importhumansize9>>>humansize.approximate_size (metadata.st_size)Ten '4.0 MiB' One>>>
8. Python Custom Module Humansize