python3-cookbook to each bar in question, solutions and discusses the three components discussed Python3 optimal solution in certain problems, or is to explore data structure, functions, etc. characteristics of certain problems in itself Python3 on how to better use. For this book to deepen understanding and enhance the Python programming Python3 the ability to have a significant help, especially on how to improve the performance of Python programs will have a good help if you have time to look at is strongly recommended.
This article is the study notes, text content only according to their operational needs and usually use the written part of the contents of the book, and most of the sample code in the text directly attached to the original code, though, the code most of them are on the Python3.6 environment after the verification. Programming focus will be different in different areas, are interested can see the full text.
Cookbook-python3: https://python3-cookbook.readthedocs.io/zh_CN/latest/index.html
13.7 copy or move files and directories
When using shutil.copytree copy the folder, you can use it ignore parameter to specify the rule is ignored, the parameter value can be a function, which accepts a directory name and file name list and return a list of names to be ignored, can is shutil.ignore_patterns ignore specified rules.
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 Creating and extracting archives
If you simply unzip or archive, use shutil.unpack_archive and shutil.make_archive it.
>>> 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 reads the configuration file
For the configuration data, the configuration data particular user, to be recorded in a special configuration files, such as ini file, for ini files, can use the built configparser module for reading and writing.
Here are just listed some simple read and write operations, configparser more information about the module, you can view the official documentation, or can refer to my notes: https://www.cnblogs.com/guyuyun/p/10965125.html
config.ini document reads as follows:
; 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 =================================
Reads the configuration file:
>>> 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 \================================= >>>
Modify the configuration file:
Cfg.set >>> ( ' Server ' , ' Port ' , ' 9000 ' ) >>> cfg.set ( ' Debug ' , ' log_errors ' , ' False ' ) >>> Import SYS >>> cfg.write ( sys.stdout) # If you want to modify the contents of the update to the file, sys.stdout here to replace the corresponding file
13.15 start a WEB browser
webbrowser module can be used to launch a browser and platform independent. The default system default browser, support for other browsers can view the official documentation.
>>> Import the WebBrowser >>> webbrowser.open ( ' http://www.python.org ' ) # default browser to open url True >>> webbrowser.open_new ( ' http://www.python.org ' ) # open url in a new browser window True >>> webbrowser.open_new_tab ( ' http://www.python.org ' ) # opens in a new browser tab url True >>> C = webbrowser.get ( ' Firefox ' ) # specified browser to open url >>> c.open ( ' http://www.python.org ') True