Python traverses local file system by size

Source: Internet
Author: User
Tags iterable rar readfile zip in python

This is a small example of how to use Python to traverse the local filesystem and sort the files by size from small to large

In this example, several functions of Python's built-in and OS modules are used primarily:

Os.walk (): This method is used to traverse the specified file directory, returning a ternary tuple (Dirpath, dirnames, filenames), where Dirpath is the current directory path, dirnames the folder under the current path, Filenames is the file under the current path
Os.path.join (): Can be used to connect directories and file names, so you can get a full path to a file
Os.path.getsize (): Gets the file size of the file, in conjunction with Os.path.join (), if passed in as the folder path, returns 0L
Sorted: Iterate over an items, and then return a new sorted list without affecting the original object



With these functions, traversing the local file is very simple, with the first three functions not described in detail,

This is mainly about the fourth function sorted usage:



Before speaking sorted, first introduce iterable, Chinese meaning is iterator

1. The explanation for iterable in the Python help document is that Iteralbe refers to an object that can return one of its members at one time.

Iteralbe mainly includes 3 categories:
The first class is all sequence types, such as list (lists), STR (strings), tuple (tuples).
The second class is a number of dict types, such as a dictionary, file, and so on.
The third class is the object of any class you define that contains the __iter__ () or __getitem__ () method.


2. The explanation of the sorted method in Python:

Sorted (iterable[, key][, reverse])

Function: Return a new sorted list from the the items in iterable.

Where key, and reverse are optional parameters


Key specifies a comparison function that receives a parameter to extract a keyword for comparison from the buy list element: For example, Key=str.lower. The default value is None (direct comparison Element)

Reverse is a Boolean value. If set to True, the list elements are sorted in reverse order.

There is also a CMP parameter in the original version, which is now removed, and the compatibility scenario is to use Functools.cmp_to_key () to convert the CMP function to a key function.

Key returns a lambda, the so-called lambda is an anonymous small function, lambda d:d[1] corresponds to the code is

def (d):
return d[1]

corresponding to the dictionary, which is to return the value in the Dictionary key value pair, D[0] represents the key, and the dictionary use sorted will return a list

Well, the basic functions are all finished, and here's the corresponding code for the example:

#-*-coding:utf-8-*-
Import OS
Import Os.path

FilePath = ' D:\temp '

FileList = []
Filemap = {}
Size = 0

# Traverse filepath files, folders (including subdirectories)
For parent, Dirnames, filenames in Os.walk (FilePath):
For dirname in Dirnames:
Print (' Parent is%s ', dirname is%s '% (parent, dirname))

For filename in filenames:
Print (' parent is%s, filename is%s '% (parent, filename)
Print (' The full name of the '%s '% os.path.join (parent, filename))

Size = Os.path.getsize (os.path.join (parent, filename))
Filemap.setdefault (os.path.join (parent, filename), size)

Print ("All size is%d"% size)

b = Sorted (Filemap.items (), Key=lambda d:d[1], Reverse=false)
For filename, size in B:
Print ("filename is%s, with size is%d"% (filename, size))

The approximate input is as follows:

Parent is D:\temp, DirName is 123
Parent is D:\temp, DirName is Java
Parent is D:\temp, the filename is Chydb_14.3_xiazaiba.zip
The full name of the ' file is D:\temp\chydb_14.3_XiaZaiBa.zip

Parent is D:\temp, the filename is Drivergenius_green1.rar
The full name of the ' file is D:\temp\DriverGenius_green1.rar

Parent is D:\temp, the filename is firefox39.7z
The full name of the ' file is D:\temp\Firefox39.7z
... Omitted



python way to traverse folders and read and write files

1, read all the files in the specified directory
2, read the specified file, output file contents
3. Create a file and save it to the specified directory

Python write code simple and efficient, to achieve the above function only 40 lines of code ~ Yesterday in Java wrote a write, create, copy, rename the file to nearly 60 lines of code;

But the price of simplicity is sacrificing a little bit of speed, but as hardware performance increases, the difference in speed will become smaller and larger, until humans are unaware

#-*-Coding:utf-8-*-

'''
1, read all the files in the specified directory
2, read the specified file, output file contents
3. Create a file and save it to the specified directory
'''
Import OS

# traverse the specified directory, showing all file names under the directory
def eachfile (filepath):
Pathdir = Os.listdir (filepath)
For Alldir in Pathdir:
Child = Os.path.join ('%s%s '% (filepath, alldir))
Print Child.decode (' GBK ') #. Decode (' GBK ') is to solve the problem of garbled Chinese display

# Read the contents of the file and print it
def readFile (filename):
fopen = open (filename, ' R ') # R stands for read
For Eachline in fopen:
Print reads the contents as follows: ", Eachline
Fopen.close ()

# Enter multiple lines of text, write to the specified file and save to the specified folder
def writefile (filename):
fopen = open (filename, ' W ')
print "\ r", please enter multiple lines of text "," (enter. # Return to save)
While True:
ALine = Raw_input ()
If ALine!= ".":
Fopen.write ('%s%s '% (ALine, os.linesep))
Else
Print "File Saved!"
Break
Fopen.close ()

if __name__ = = ' __main__ ':
FilePath = "D:\\filedemo\\java\\myjava.txt"
Filepathi = "d:\\filedemo\\python\\pt.py"
FILEPATHC = "c:\\"
Eachfile (FILEPATHC)
ReadFile (FilePath)
WriteFile (Filepathi)


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.