【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')
日志归档为:

【Python】Python的安装与个人使用记录的更多相关文章
- 【Python①】python简介,安装以及配置
今天开始学习python,将一些心得和知识点记录下来,如有疏漏或表达问题,欢迎指正.后面所有代码均为Python 3.3.2版本(运行环境:Windows7)编写. 附:2014年8月TIOBE编程语 ...
- Python介绍、安装、使用
Python介绍.安装.使用 搬运工:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Python语言介绍 说到Python语言,就不得不说一下它的创始人Guido van Rossu ...
- python解释器的安装;python2与python3同时在环境变量中时的解决方案
新文档 html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,addres ...
- PythonDay02——编程语言、python介绍以及安装解释器、运行程序的两种方式、变量
一.编程语言 1.1 机器语言:直接用计算机能理解的二进制指令编写程序,直接控制硬件 1.2 汇编语言:用英文标签取代二进制指令去编写程序,本质也是直接控制硬件 1.3 高级语言:用人能理解的表达方式 ...
- Python 3 的安装
python 3 的安装: 背景: 之前都是在Pychram上写,我的windows下的python版本是3.5,今天要把一个小脚本上到生产环境上. 无奈我服务器上的python版本是2.6.6.所以 ...
- ubuntu配置默认python版本并安装pip
ubuntu 16.04本身是自带python的,他本身是自带2.X和3.X,两个版本,默认的是2.X.这里记录一下如果在版本间切换以及如何把python版本切换到3.X下的方法. 1.查看Ubunt ...
- Python 之 PyMySQL 安装和使用
Python具有内置的SQLite支持. 在本节中,我们将学习使用MySQL的相关概念和知识. 在早期Python版本一般都使用MySQLdb模块,但这个MySQL的流行接口与Python 3不兼容. ...
- Python学习笔记之基础篇(-)python介绍与安装
Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...
- Window下Python+CUDA+PyTorch安装
1 概述 Windows下Python+CUDA+PyTorch安装,步骤都很详细,特此记录下来,帮助读者少走弯路. 2 Python Python的安装还是比较简单的,从官网下载exe安装包即可: ...
- Python 2/3 安装与运行环境设置
Python 2/3 安装与运行环境设置: 1.Python 软件源:https://www.python.org/ 下载Win版本 https://www.python.org/downloa ...
随机推荐
- POJ 1655 Balancing Act (求树的重心)【树形DP】(经典)
<题目链接> 题目大意:给你一棵树,任意去除某一个点后,树被分成了几个联通块,则该点的平衡值为所有分成的连通块中,点数最大的那个,问你:该树所有点中,平衡值最小的那个点是什么? 解题分析: ...
- 利用ApiPost接口调试与文档生成工具,提升前、后端工作效率
什么是ApiPost? 场景1: 对于我们后端程序员,常常会写一些接口(APIs),但是在前端尚未调用之前,我们必须先自己测试下这个接口是不是正确返回了预定结果.对于一个GET请求的接口还好,我们可以 ...
- JavaEE - 20181225
作者:沈世钧链接:https://www.zhihu.com/question/305924723/answer/557800752来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- pojA Star not a Tree?
题目链接 pojA Star not a Tree? 题解 啊,模拟退火是个好东西 模拟退火即可 代码 #include<cmath> #include<cstdio> #in ...
- Python3自然语言(NLTK)——语言大数据
NLTK 这是一个处理文本的python库,我们知道文字性的知识可是拥有非常庞大的数据量,故而这属于大数据系列. 本文只是浅尝辄止,目前本人并未涉及这块知识,只是偶尔好奇,才写本文. 从NLTK中的b ...
- extend与append的区别
''' list 的两个方法extend 和 append 看起来类似,但实际上完全不同. extend接受一个参数,这个参数,总是一个list,并把list中的每个元素添加到原list中 appen ...
- boostrap常用的类
1.col-md-push-3 :向右移动3 2.col-md-pull -9 : 向左移动9 3.clearfix: 清除元素浮动问题 4. col-md-offset-3: 向右偏移 5.pul ...
- SVN Error:请求的名称有效并且在数据库中找到,但是它没有相关的正确的数据来被解析
同事安装配置完Svn后一直down不下来文件,报错内容如下: Administrator 18:07:27 Checkout from https:/svn/web, revision HEAD, ...
- android: 碎片的demo
现在你已经将关于碎片的重要知识点都掌握得差不多了,不过在灵活运用方面可能还有 些欠缺,因此又该进入我们本章的最佳实践环节了. 前面有提到过,碎片很多时候都是在平板开发当中使用的,主要是为了解决屏幕空间 ...
- 【docker】docker部署spring boot服务,但是docker logs查看容器输出控制台日志,没有日志打印,日志未打印,docker logs不打印容器日志
如题: docker部署spring boot服务,但是docker logs查看容器输出控制台日志,没有日志打印,日志未打印,docker logs不打印容器日志 场景再现: docker部署并启动 ...