【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 ...
随机推荐
- 008.Docker Flannel+Etcd分布式网络部署
一 环境准备 1.1 Flannel概述 Flannel是一种基于overlay网络的跨主机容器网络解决方案,即将TCP数据包封装在另一种网络包里面进行路由转发和通信,Flannel是CoreOS开发 ...
- codeforces-1111
https://www.cnblogs.com/31415926535x/p/10397007.html codeforces 537 div2 A 题意就是给你两个字符串,然后如果s,t的对应位上的 ...
- shell seq 用法
seq [OPTION]... LASTseq [OPTION]... FIRST LASTseq [OPTION]... FIRST INCREMENT LAST seq 1000 ‘起始默认是 ...
- IBM NOTES
归档含义 邮件容量超过指定的邮件服务器的容量时,会受到警告信息.选择 Archive-Settings 可以设置邮件备份的规则,把邮箱里的部分邮件转移到本地的另一个指定文件夹(如 图 4).当您需要做 ...
- 4572: [Scoi2016]围棋 轮廓线DP KMP
国际惯例的题面:这种题目显然DP了,看到M这么小显然要状压.然后就是具体怎么DP的问题.首先我们可以暴力状压上一行状态,然后逐行转移.复杂度n*3^m+3^(m*2),显然过不去. 考虑状态的特殊性, ...
- bzoj 3811: 玛里苟斯
3811: 玛里苟斯 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 190 Solved: 95[Submit][Status][Discuss] ...
- Chrome for Mac键盘快捷键!来自Google Chrome官网!
⌘-N 打开新窗口. ⌘-T 打开新标签页. ⌘-Shift-N 在隐身模式下打开新窗口. 按 ⌘-O,然后选择文件. 在 Google Chrome 浏览器中打开计算机中的文件. 按住 ⌘ 键,然后 ...
- CentOS+Nginx+PHP 前端部署
都说Nginx比Apache性能优越,一直没有时间装测试,今天终于有时间装上试试性能了,其实Nginx的安装非常简单,具体流水步骤记录如下: 1.系统环境: ===================== ...
- (netty宝贵知识)
例子:https://segmentfault.com/a/1190000013122610?utm_source=tag-newest#articleHeader0 netty官方文档http:// ...
- __x__(2)0905第二天__计算机软件和硬件
计算机(Computer)由硬件和软件组件,没有软件的计算机称为 裸机, 计算机的软件包括操作系统(OS)和应用软件(Software). 操作系统(Operating System,简称OS) 是管 ...