码迷,mamicode.com
首页 > 编程语言 > 详细

《可爱的Python》读书笔记(二)

时间:2018-03-19 22:43:04      阅读:246      评论:0      收藏:0      [点我收藏+]

标签: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》读书笔记(二)

标签:walk   this   

原文地址:http://blog.51cto.com/9473774/2088763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!