python3-cookbook笔记:第十三章 脚本编程与系统管理

python3-cookbook中每个小节以问题、解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构、函数、类等特性在某类问题上如何更好地使用。这本书对于加深Python3的理解和提升Python编程能力的都有显著帮助,特别是对怎么提高Python程序的性能会有很好的帮助,如果有时间的话强烈建议看一下。
本文为学习笔记,文中的内容只是根据自己的工作需要和平时使用写了书中的部分内容,并且文中的示例代码大多直接贴的原文代码,当然,代码多数都在Python3.6的环境上都验证过了的。不同领域的编程关注点也会有所不同,有兴趣的可以去看全文。
python3-cookbook:https://python3-cookbook.readthedocs.io/zh_CN/latest/index.html

13.7 复制或者移动文件和目录

在使用shutil.copytree进行文件夹的拷贝时,可以使用它的ignore参数来指定忽略的规则,参数值可以是一个函数,此函数接受一个目录名和文件名列表并返回要忽略的名称列表,也可以是shutil.ignore_patterns指定的忽略规则。

def ignore_pyc_files(dirname, filenames):
    return [name in filenames if name.endswith('.pyc')]

shutil.copytree(src, dst, ignore=ignore_pyc_files)

shutil.copytree(src, dst, ignore=shutil.ignore_patterns('*~', '*.pyc'))

13.8 创建和解压归档文件

如果只是简单的解压或者压缩文档,使用shutil.unpack_archive和shutil.make_archive就可以了。

>>> import shutil
>>> shutil.unpack_archive('Python-3.3.0.tgz')

>>> shutil.make_archive('py33','zip','Python-3.3.0')
'/Users/beazley/Downloads/py33.zip'
>>>
>>># 支持的格式如下
>>> shutil.get_archive_formats()
[('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"),
 ('tar', 'uncompressed tar file'), ('zip', 'ZIP file')]
>>>

13.10 读取配置文件

对于配置数据,特别是用户配置数据,应该记录在专门的配置文件中,比如ini文件,对于ini文件,可以使用内置的configparser模块来进行读写。

这里只是列了些简单的读写操作,关于configparser模块更多信息,可以查看官方文档,或者可以参考下我的笔记:https://www.cnblogs.com/guyuyun/p/10965125.html

config.ini文件内容如下:

; config.ini
; Sample configuration file

[installation]
library=%(prefix)s/lib
include=%(prefix)s/include
bin=%(prefix)s/bin
prefix=/usr/local

# Setting related to debug configuration
[debug]
log_errors=true
show_warnings=False

[server]
port: 8080
nworkers: 32
pid-file=/tmp/spam.pid
root=/www/root
signature:
    =================================
    Brought to you by the Python Cookbook
    =================================

读取配置文件:

>>> from configparser import ConfigParser
>>> cfg = ConfigParser()
>>> cfg.read('config.ini')
['config.ini']
>>> cfg.sections()
['installation', 'debug', 'server']
>>> cfg.get('installation','library')
'/usr/local/lib'
>>> cfg.getboolean('debug','log_errors')

True
>>> cfg.getint('server','port')
8080
>>> cfg.getint('server','nworkers')
32
>>> print(cfg.get('server','signature'))

\=================================
Brought to you by the Python Cookbook
\=================================
>>>

修改配置文件:

>>> cfg.set('server','port','9000')
>>> cfg.set('debug','log_errors','False')
>>> import sys
>>> cfg.write(sys.stdout)  # 如果想将修改内容更新到文件中,将这里的sys.stdout替换为对应文件即可

13.15 启动一个WEB浏览器

webbrowser模块能被用来启动一个浏览器,并且与平台无关。默认使用系统默认的浏览器,其他支持的浏览器可以查看官方文档。

>>> import webbrowser
>>> webbrowser.open('http://www.python.org')  # 以默认浏览器打开url
True
>>> webbrowser.open_new('http://www.python.org')  # 在一个新的浏览器窗口打开url
True
>>> webbrowser.open_new_tab('http://www.python.org')  # 在浏览器的新标签中打开url
True
>>> c = webbrowser.get('firefox')  # 使用指定的浏览器打开url
>>> c.open('http://www.python.org')
True

猜你喜欢

转载自www.cnblogs.com/guyuyun/p/12381306.html