【Python】Python的安装与个人使用记录

下载

官网上下载,目前,最新版是Python3,基于项目需求,我们使用的是Python2。
我是在CentOS上安装,下载的是Python-2.7.9.tgz

安装

tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
./configure
make
make install

测试

安装完毕,用python测试,如果看到版本信息说明安装成功。用exit()退出交互模式。

简单的语法

日期

#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime;

today = datetime.date.today();
print('today : ' + str(today));

yesterday = datetime.date.today() - datetime.timedelta(days=1);
print('yesterday : ' + str(yesterday));

结果:

today : 2017-07-09
yesterday : 2017-07-08

ZIP文件解压

#!/usr/bin/python
# -*- coding: utf-8 -*-

import zipfile

def unzip(file_path, extract_to_path):
    if not zipfile.is_zipfile(file_path):
        print('The file is not zip file')
        return

    zip_file = zipfile.ZipFile(file_path, 'r')
    for file in zip_file.namelist():
        print(file) # 解压的文档
        zip_file.extract(file, extract_to_path)

unzip('D:/python27_workspace/myzip.zip', 'D:/python27_workspace/unzip')

读取ini文件

ini文件的内容:

[details]
name = Nick Huang

读取ini文件内容:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import ConfigParser

config_parser = ConfigParser.ConfigParser()
config_parser.read('D:/python27_workspace/ini_reading/info.ini')
name = config_parser.get('details', 'name')
print name

日志的基础用法

#!/usr/bin/python
# -*- coding: utf-8 -*-

import logging
import datetime

my_formatter = logging.Formatter('%(asctime)s %(filename)s[%(lineno)d] %(levelname)s : %(message)s')

file_handler = logging.FileHandler('D:/python27_workspace/logging/mylog_' + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + '.log', mode='w')
file_handler.setFormatter(my_formatter)

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(my_formatter)

logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)

logger.debug('hello')
logger.info('hello')
logger.error('hello')

结果:

2017-07-14 17:47:56,871 logging-exercise.py[21] INFO : hello
2017-07-14 17:47:56,871 logging-exercise.py[22] ERROR : hello

日志公用配置

详细文档说明见logging.config — Logging configuration。例子如下。

配置文件如下:

[loggers]
keys=root

[handlers]
keys=streamHandler,fileHandler

[formatters]
keys=myStandardFormatter

[logger_root]
level=DEBUG
handlers=streamHandler,fileHandler

# 控制台输出配置
[handler_streamHandler]
class=StreamHandler
level=DEBUG
formatter=myStandardFormatter
args=(sys.stdout,)

# 文件输出配置(这里的'S', 1, 0,设置每1秒滚动一个配置文件,并不删除文件(这个配置需求可能并不是大家需要的,所以特别指出))
[handler_fileHandler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=myStandardFormatter
args=('D:/python27_workspace/logging-common-config/mylog.log', 'S', 1, 0)

## 输出格式
[formatter_myStandardFormatter]
format=%(asctime)s %(filename)s[%(lineno)d] %(levelname)s : %(message)s
datefmt=
class=logging.Formatter

程序如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import logging
import logging.config

logging.config.fileConfig("logging.conf")

logger = logging.getLogger("root")

logger.debug('hello')
logger.info('hello')
logger.error('hello')

日志归档为:

原文地址:https://www.cnblogs.com/nick-huang/p/7143324.html