Pyhon obtains the statistics of the folder size,

Source: Internet
Author: User

Pyhon obtains the statistics of the folder size,

Statistics on the folder Size Using Python: mainly through several methods in import OS.

-- Zero

Python OS file/directory method

First, we will introduce several functions used and write a small demo for ease of understanding.

 

OS. getcwd ()

Returns the current path.

OS. listdir (path)

Returns the folders and files in the current path (do not go down to Level 2 recursion ).

OS. path. join ()

The join () method is used to concatenate elements in a sequence with specified characters to generate a new string.

OS. path identifies join () as a path and automatically complements '\'/'According to the system '\''/'.

OS. path. getsize (path) returns the file size.

1 #-*-encoding: UTF-8-* _ 2 import OS 3 4 path = OS. getcwd () # obtain the current path 5 for dirs in OS. listdir (path): 6 print dirs 7 8 file_name = ".txt" 9 path = OS. path. join (path, file_name) 10 print path11 size = OS. path. getsize ("E: \ PythonEclipse \ PythonStudy \ OS. listdir \ listdirs. py ") 12 print size

Output result:

1 A2 B3 C4 file.txt 5 listdirs. py6 E: \ PythonEclipse \ PythonStudy \ OS. listdir \ .txt
7 303

OS. walk (top [, topdown = True [, onerror = None [, followlinks = False])

Top -- each folder under the root directory (including its own) generates 3-tuples (dirpath, dirnames, filenames) [Folder path, folder name, and file name ].

Topdown -- optional, True or unspecified. The 3-tuples in a directory are generated first (the directory is top-down) than the 3-tuples in any of its subfolders ). If topdown is False, the 3-tuples of a directory are generated after the 3-tuples of any of its subfolders (the directory is bottom-up ).

Onerror -- (optional) indicates a function. It calls an OSError instance as a parameter. After this error is reported, continue the walk or throw an exception to terminate the walk.

Followlinks -- if it is set to true, the directory is accessed through soft links.

1 # -*- encoding: utf-8  -*_2 import os3 4 for root, dirs, filename in os.walk(os.getcwd()):5     print root6     print dirs7     print filename

Output result:

 1 E:\PythonEclipse\PythonStudy\os.listdir 2 ['A', 'B', 'C'] 3 ['file.txt', 'listdirs.py'] 4 E:\PythonEclipse\PythonStudy\os.listdir\A 5 [] 6 [] 7 E:\PythonEclipse\PythonStudy\os.listdir\B 8 [] 9 ['file.txt']10 E:\PythonEclipse\PythonStudy\os.listdir\C11 []12 []

Function Implementation ideas:

To obtain the folder size, you only need to traverse all the files under the file and obtain the sum of all the file sizes.

In this example, the folder size in the current directory does not include the file size in the current file.

Note the format of python encoding.

1 #-*-encoding: UTF-8-*-2 import OS 3 rootdir = OS. getcwd () # obtain the current path 4 5 rootdir = rootdir. decode ('gbk') 6 x = u'' ..csv '7 f = open (OS. path. join (rootdir, x), "w +") 8 for dirname in OS. listdir (rootdir): # obtain all folders and files in the level-2 Directory 9 Dir = OS. path. join (rootdir, dirname) # Fill 10 count = 011 if (OS. path. isdir (Dir): # determine whether the directory is 12 for r, ds, files in OS. walk (Dir): # traverse the root of all the files in the directory, each folder in the directory (including its own), generate 3-tuples (dirpath, dirnames, filenames) [Folder path, folder name, file name] 13. file in files: # traverse all files. 14 size = OS. path. getsize (OS. path. join (r, file) # Get the file size 15 count + = size16 if (count/1024.0/1024.0) <1024 ): 17 print Dir + '\ t' +' %. 2f '% (count/1024.0/1024.0) + 'mb' 18 f. write (Dir. encode ("gbk") + ',' + '%. 2f '% (count/1024.0/1024.0) + 'mb' + '\ n') 19 else: 20 print Dir +' \ t' + '%. 2f '% (count/1024.0/1024.0/1024.0) + 'gb' 21 f. write (Dir. encode ("gbk") + ',' + '%. 2f '% (count/1024.0/1024.0/1024.0) + 'gb' +' \ n') 22 else: 23 continue24 f. close ()

Related Article

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.