First, print the most commonly used 10 Linux commands
#!/usr/bin/python#coding=utf-8import osfrom Collections Import Counterc = Counter () with open (Os.path.expanduser (' ~/. Bash_history ') as F:for line in f:cmd = Line.strip (). Split () if Cmd:c[cmd[0]]+=1print C.most_common (10)
The effect is as follows:
Second, find the duplicate files in the directory
#!/usr/bin/python#coding=utf-8from __future__ import print_functionimport sysimport Hashlibimport osimport fnmatchchunk_size=8192def is_file_match (filename,patterns):for Pattern in patterns:if fnmatch.fnmatch (Filename,pattern): Return truereturn falsedef find_specific_files (root,patterns=[' * '],exclude_dirs=[]):for root,dirnames,filenames in Os.walk (Root): For filename in filenames:if is_file_match (filename,patterns):yield Os.path.join (root,filename) For d in exclude_dirs:if d in dirnames: Dirnames.remove (d) def get_chunk (filename): with open (filename) as f:while true:chunk= F.read (chunk_size) if not chunk:breakelse:yield chunkdef get_file_checksum (filename): h= HASHLIB.MD5 () for chunk in get_chunk (filename): h.update (Chunk) return h.hexdigest () def main (): Sys.argv.append ("") directorY=sys.argv[1]if not os.path.isdir (directory):raise systemexit ("{0} is not A directory. Format (directory) record={}for item in find_specific_files (directory): Checksum=get_file_checksum (item) if checksum in record:print (' find duplicate Files: {0} vs {1} '. Format (record[checksum],item)) else:record[checksum]=itemif __name__ == ' __main__ ': Main ()
The effect is as follows:
Python Learning-Print the most commonly used 10 Linux commands and find duplicate files in directories