In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to achieve the du command under windwos, the content is very detailed, interested friends can refer to, hope to be helpful to you.
This article is reproduced from the sheepskin scroll website and may not be forwarded without authorization.
Du can display the amount of disk space occupied by all files in each directory in subdirectories under a specified directory.
However, the du command only supports unix/liunx, he does not support windows, how to get him to support windows.
? In the past maintenance process, especially when cleaning logs, I used this command very frequently, because it can quickly locate the size of directories and files, which is a powerful tool for cleaning disks.
If we maintain the windows server, check the directory size to select a directory, and then right-click to check the directory size, which is too inefficient. So we can consider making our own command tool, simply implementing the du command, and sorting the directory size, like this:
Using python to realize process Analysis
1. Enter path to the user:
We set up a simple startup function start, which allows special users to type path to users.
Def start ():
2. According to path, find the catalog file, count the size, and sort it:
We set up a list function. After accepting the path, we find out the directory and file, put the directory name, file name and size data into the dictionary, and finally sort the dictionary.
Def list (path):
3. Statistical file size:
Def getSize (file):
Start- handles user input
# 2. Define the function entered by the user. If the user enters the nmax N, the program will be stopped. The input path will call the list function, statistical directories and files.
Def start ():
IsNext=1 # defines a variable of whether to continue letting the user enter path
While isNext==1:#isNext==1, continue to loop the process of input and processing.
Path = raw_input ("enter path (d:\\), stop enter nmax N:") # raw_input lets the user enter and gets the input
If path=='n' or path=='N':# if the user typed dead nUniverse isNext assignment 0, it will not be executed after execution.
IsNext=0
Else:
List (path) # instead, call the list (path) function and hand over the path to list for processing
# 1. When you start executing this file, start with start as the main function
If _ _ name__=='__main__':
Start ()
List- lists directories and files under pATH
# 3. List the directories and files under path
Def list (path):
Dirs= {} # defines a dictionary that stores directories
# list all the first-level directories and files under path
For dp in os.listdir (path):
# 3.1. Determine whether dp is a directory
If os.path.isdir (os.path.join (path,dp)):
Dir_size=0# temporary storage directory size
# os.walk traverses the directory to get all the files
For root,dir,files in os.walk (os.path.join (path,dp)):
List of files in each directory of for name in files:#
Try:
# accumulate the size of each file under the folder
Dir_size + = getsize (os.path.join (root,name))
Except Exception, e:
Pass
Dirs [dp] = dir_size# add directory and size
# 3.2. File processing
Elif os.path.isfile (os.path.join (path,dp)):
Try:
# calculate the file size and add it to the dir dictionary
Dirs [dp] = getsize (os.path.join (path,dp))
Except Exception, e:
Pass
# sort directory or file size
Ds=sorted (dirs.iteritems (), key=lambda dvd [1], reverse=True)
# output directory size
For x in ds:
D = x [0]
Size=x [1]
Size2=''
If size > = 1073741824:
Size2=str (size/1024/1024/1024) +'G'
Elif size > = 106496 and size=106496 and size
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 249
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.