Pyhon get the size statistics of a folder

Source: Internet
Author: User
Python OS File/directory method

First introduced the use of several functions, wrote a small demo easy to understand.

OS.GETCWD ()

Returns the current path.

Os.listdir (PATH)

Returns the folder and file under the current path (not recursively down to level two).

Os.path.join ()

The join () method is used to generate a new string from the elements in the sequence with the specified character connection.

Os.path let join () recognize as a path, according to the system auto-completion ' \ '/'.

Os.path.getsize (path) return file size

#-*-Encoding:utf-8  -*_import ospath = OS.GETCWD () # Gets the current path for dirs in Os.listdir (path):    Print dirsfile_name = " Path is padded with. txt "path = Os.path.join (path,file_name) Print pathsize = Os.path.getsize (" E:\PythonEclipse\PythonStudy\ os.listdir\listdirs.py ") Print size

Output Result:

Abcfile.txtlistdirs.pye:\pythoneclipse\pythonstudy\os.listdir\ path is padded. txtA303

Os.walk (top[, topdown=true[, onerror=none[, Followlinks=false]])

Top-Every folder in the root directory (including itself), resulting in 3-tuple (Dirpath, dirnames, filenames) "Folder path, folder name, file name".

Topdown--optional, true or unspecified, a 3-tuple of a directory will be generated first (directory top-down) than the 3-tuple of any of its subfolders. If Topdown is False, a 3-tuple of a directory will be generated after the 3-tuple of any of its subfolders (directory bottom-up).

OnError--optional, is a function; It is called when there is a parameter, an OSError instance. After reporting this error, continue to walk, or throw exception to terminate walk.

Followlinks--set to True to access the directory through a soft link.

#-*-Encoding:utf-8  -*_import osfor root, dirs, filename in Os.walk (OS.GETCWD ()):    print root    print dirs
  
   print filename
  

Output Result:

e:\pythoneclipse\pythonstudy\os.listdir[' A ', ' B ', ' C ' [' file.txt ', ' listdirs.py ']e:\pythoneclipse\pythonstudy\ os.listdir\a[][]e:\pythoneclipse\pythonstudy\os.listdir\b[][' file.txt ']e:\pythoneclipse\pythonstudy\os.listdir \c[][]

function Realization Idea:

Need to get the size of the folder, just traverse all the files under the file, get all the file size sum.

Here is the implementation of the current directory, the folder size does not include the file size under the current file.

Python's encoded format needs to be noted.

#-*-encoding:utf-8-*-import osrootdir = OS.GETCWD () #获取当前路径rootdir = Rootdir.decode (' GBK ') x = U ' statistical file size. CSV ' F = open (Os.path.join (rootdir,x), "w+") for dirname in Os.listdir (rootdir): #获取二级目录所有文件夹与文件 Dir = Os.path.join (roo Tdir, dirname) #路径补齐 count = 0 if (os.path.isdir (Dir)): #判断是否为目录 for R, DS, files in Os.walk (dir      ): #遍历目录下所有文件根, each folder in the directory (including itself), producing 3-tuple (Dirpath, dirnames, filenames) "Folder path, folder name, file name" for files: #遍历所有文件 size = Os.path.getsize (Os.path.join (r, file)) #获取文件大小 count + = size if ( (count/1024.0/1024.0) < 1024x768): print Dir + ' \ t ' + '%.2f '% (count/1024.0/1024.0) + ' MB ' F.write (Dir. Encode ("GBK") + ', ' + '%.2f '% (count/1024.0/1024.0) + ' MB ' + ' \ n ') else:print Dir + ' \ t ' + '%.2f '% (coun t/1024.0/1024.0/1024.0) + ' GB ' F.write (Dir.encode ("GBK") + ', ' + '%.2f '% (count/1024.0/1024.0/1024.0) + ' GB ' + ' \ n ') Else:       Continuef.close () 

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.