"Cute Python" reading notes (ii)

Source: Internet
Author: User
Tags gpg

Find it! Don't first think about creating--python is self-sufficient.


Continue to analyze yesterday's content

Little white Ask the question: How to read the file list information in the specified optical drive "E:"?

The document is a system thing.

Analysis: System → operating system →operating System→os module!

>>> Import os>>> os.listdir (' e:\\ ') ['. Discinfo ', '. Treeinfo ', ' centos_buildtag ', ' EFI ', ' EULA ', ' GPL ', ' images ', ' isolinux ', ' release-notes-en-us.html ', ' rpm-gpg-key-centos-6 ', ' rpm-gpg-key-centos-debug-6 ', ' Rpm-gpg-key-centos-security-6 ', ' rpm-gpg-key-centos-testing-6 ', ' TRANS. TBL ', ' [BOOT] '


Little White raises the question: How can I automatically "scan" all of the files and directory information in the entire disc "at once"?

Small white think: can be based on the information of each level directory again and again call Os.listdir (), all levels of directory information are reported.

By Walk (): Use the

(There are two walk () respectively Os.path.walk () and Os.walk () the former have been removed from Python3)

#-*-Coding:utf-8-*-import osdef cdwalker (CDROM, cdcfile): Export = "" For root, dirs, files in Os.walk (CDROM): #print (root, dirs, files) export + = "\%s;%s;%s"% (root, dirs, files) #print (export) Open (Cdcfile , ' W '). Write (export) cdwalker (' e:\\ ', ' CD1.CDC ') cdwalker (' e:\\ ', ' CD2.CDC ')


Little White got the first Python function and successfully ran it two times, recording the contents of the same disc to 2 different files "CD1.CDC" and "CD2.CDC".

Take a look at the above code using the + connection of the string, below is the use of join. The join of a string is more efficient than the + operation. Because the object's repeated +, than one-time built-in processing, to waste more resources.


#-*-Coding:utf-8-*-import osdef cdwalker (CDROM, cdcfile): Export = [] for root, dirs, files in Os.walk (CDROM): Export.append ("\ n%s;%s;%s"% (root, dirs, files)) open (Cdcfile, ' W '). Write (". Join (export)) cdwalker (' e:\\ ', ' CD3 . CDC ')


Small exercise:

Reads this.txt content, strips empty lines and comment lines, sorts them in the behavior unit, and outputs the results as TheZenofPython.txt.

#The Zen of Python, by Tim Peters


Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren ' t special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should is one--and preferably only one--obvious the-do it.

Although that is obvious at first unless you ' re Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it could be a good idea.

Namespaces is one honking great idea – let's do more than those!

# -*- coding: utf-8 -*-result = []with open (' This.txt ')  as f:     for line in f.readlines ():                     #  read each line in turn          line = line.strip ()                         #  remove trailing blanks for each outfit         if line.startswith (' # ')  or not line:       #  determine if it is a blank line or comment line              continue        result.append (line) Result.sort ()                                        #  Sort result Print (result) open (' TheZenOfPython.txt ',  ' W '). Write ('%s '  %  ' \ n '). Join (Result)   #  save into result file >>> [' Although never is often better than *right* now. ',  ' Although  practicality beats purity. ',  ' although that way may not be  Obvious at first unless you ' Re dutch. ',  ' Beautiful is better than  ugly. ',  ' complex is better than complicated. ',  ' Errors should  never pass silently. ',  ' explicit is better than implicit. ',  ' Flat  is better than nested. ',  ' if the implementation is easy to  explain, it may be a good idea. ',  ' if the implementation  is hard to expLain, it ' S a bad idea. ',  ' In the face of ambiguity, refuse  the temptation to guess. ',  ' namespaces are one honking great  idea -- let ' s do more of those! ',  ' Now is better than  never. ',  ' readability counts. ',  ' Simple is better than complex. ',   ' sparse is better than dense ',  ' Special cases aren ' t special  enough to break the rules. ",  ' There should be one-- and  preferably only one --obvious way to do it. ',  ' Unless  explicitly silenced. ']

The contents of the TheZenofPython.txt are:

Although never is often better than *right* now.

Although practicality beats purity.

Although that is obvious at first unless you ' re Dutch.

Beautiful is better than ugly.

Complex is better than complicated.

Errors should never pass silently.

Explicit is better than implicit.

Flat is better than nested.

If the implementation is easy to explain, it could be a good idea.

If the implementation is hard to explain, it's a bad idea.

In the face of ambiguity, refuse the temptation to guess.

Namespaces is one honking great idea – let's do more than those!

Now is better than never.

Readability counts.

Simple is better than complex.

Sparse is better than dense.

Special cases aren ' t special enough to break the rules.

There should is one--and preferably only one--obvious the-do it.

Unless explicitly silenced.


"Cute Python" reading notes (ii)

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.