设为首页 收藏本站
查看: 3821|回复: 0

[经验分享] 《可爱的Python》读书笔记(二)

[复制链接]

尚未签到

发表于 2018-8-11 06:38:44 | 显示全部楼层 |阅读模式
  寻找吧!不要先想着创造——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>  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>
  If the implementation is easy to explain, it may be a good>
  Namespaces are one honking great># -*- 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>
  If the implementation is hard to explain, it's a bad>
  In the>
  Namespaces are one honking great>  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.
  总结:本节接触到了os模块,了解了通过+和join可以进行字符串连接,同时用到了python的文件操作方法等。初步实现了将光盘内容索引存储为硬盘上的文本文件。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-549802-1-1.html 上篇帖子: Python面向对象——继承 下篇帖子: Python将是人工智能时代的最佳编程语言
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表