python3-cookbook笔记:第十三章 脚本编程与系统管理
python3-cookbook中每个小节以问题、解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构、函数、类等特性在某类问题上如何更好地使用。这本书对于加深Python3的理解和提升Python编程能力的都有显著帮助,特别是对怎么提高Python程序的性能会有很好的帮助,如果有时间的话强烈建议看一下。 ? 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,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(py33zipPython-3.3.0) /Users/beazley/Downloads/py33.zip' >>> >>># 支持的格式如下 >>> shutil.get_archive_formats() [(bztar"bzip2'ed tar-file"),(gztargzip'ed tar-file"),(taruncompressed tar file'),1)">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 ConfigParser >>> cfg = ConfigParser() >>> cfg.read(config.ini) [] >>> cfg.sections() [] >>> cfg.get(library/usr/local/lib' >>> cfg.getboolean(log_errors) True >>> cfg.getint(port) 8080 >>> cfg.getint(nworkers) 32 >>> print(cfg.get(signature)) ================================= Brought to you by the Python Cookbook ================================= >>> 修改配置文件: >>> cfg.set(9000) >>> cfg.set(False) >>> sys >>> cfg.write(sys.stdout) # 如果想将修改内容更新到文件中,将这里的sys.stdout替换为对应文件即可 ? ? 13.15 启动一个WEB浏览器 webbrowser模块能被用来启动一个浏览器,并且与平台无关。默认使用系统默认的浏览器,其他支持的浏览器可以查看官方文档。 >>> webbrowser >>> webbrowser.open(http://www.python.org') 以默认浏览器打开url True >>> webbrowser.open_new( 在一个新的浏览器窗口打开url True >>> webbrowser.open_new_tab( 在浏览器的新标签中打开url True >>> c = webbrowser.get(firefox 使用指定的浏览器打开url >>> c.open() True ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |