1. 程式人生 > >《可愛的Python》讀書筆記(二)

《可愛的Python》讀書筆記(二)

walk this

尋找吧!不要先想著創造——Python 是自足的。


繼續分析昨天的內容

小白提出問題:如何讀取指定光驅"E:"中的文件列表信息?

行者提出:文件是系統的事兒。

分析:系統→操作系統→operating system→os模塊!

>>> import os
>>> os.listdir('E:\\')
['.discinfo', '.treeinfo', 'CentOS_BuildTag', 'EFI', 'EULA', 'GPL', 'images', 'i
solinux', 'RELEASE-NOTES-en-US.html', 'RPM-GPG-KEY-CentOS-6', 'RPM-GPG-KEY-CentO
S-Debug-6', 'RPM-GPG-KEY-CentOS-Security-6', 'RPM-GPG-KEY-CentOS-Testing-6', 'TR
ANS.TBL', '[BOOT]']


小白提出問題:如何自動地將整個光盤中的所有文件和目錄信息都“一次性地掃描”出來?

小白認為:可以根據每級目錄的信息再次不斷調用os.listdir(),將所有層次的目錄信息都逐一匯報出來。

行者提出:使用walk()

(有兩個walk()分別為os.path.walk()和os.walk()前者在Python3已被移除)

# -*- coding: utf-8 -*-
import os

def cdWalker(cdrom, cdcfile):
    export = ""
    for root, dirs, files in os.walk(cdrom):
        #print(root, dirs, files)
        export += "\n %s;%s;%s" % (root, dirs, files)
        #print(export)
    open(cdcfile, 'w').write(export)
cdWalker('E:\\', 'cd1.cdc')
cdWalker('E:\\', 'cd2.cdc')


小白獲得了第一個Python函數,並成功運行了兩次,即將同張光盤的內容記錄到2個不同的文件"cd1.cdc"和"cd2.cdc"中。

再來看看,上面的代碼使用了字符串的+連接,下面是利用join。字符串的join要比+操作效率高。因為對象的反復+,比一次性內建處理,要浪費更多的資源。


# -*- coding: utf-8 -*-
import os

def 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')


小練習:

讀取this.txt內容,去除空行和註釋行後,以行為單位進行排序,並將結果輸出為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 be one-- and preferably only one --obvious way to do it.

Although that way may not be 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 may be a good idea.

Namespaces are one honking great idea -- let's do more of those!

# -*- coding: utf-8 -*-
result = []
with open('this.txt') as f:
    for line in f.readlines():                    # 依次讀取每行
        line = line.strip()                       # 去掉每行頭尾空白
        if line.startswith('#') or not line:      # 判斷是否是空行或註釋行
            continue
        result.append(line)
result.sort()                                     # 排序結果
print(result)
open('TheZenOfPython.txt', 'w').write('%s' % '\n'.join(result))  # 保存入結果文件
>>>
['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.']

TheZenofPython.txt的內容為:

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.


《可愛的Python》讀書筆記(二)