文章目录
一、sys.exit() os._exit() exit() quit()
参考:python之 sys.exit() os._exit() exit() quit()的简单使用
1.sys.exit():
执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法。
退出程序引发SystemExit异常,(这是唯一一个不会被认为是错误的异常),如果没有捕获这个异常将会直接退出程序执行,当然也可以捕获这个异常进行一些其他操作(比如清理工作)。
sys.exit()
函数是通过抛出异常的方式来终止进程的,也就是说如果它抛出来的异常被捕捉到了的话程序就不会退出了,而是去进行一些清理工作。
SystemExit并不派生自Exception,所以用Exception捕捉不到该SystemEixt异常,应该使用SystemExit来捕捉。
该方法中包含一个参数status,默认为0,表示正常退出,其他都是异常退出。
还可以这样使用:sys.exit("Goodbye!");
一般主程序中使用此退出。
捕获到SystemExit异常,程序没有直接退出:
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit(825)
except SystemExit,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
the information of SystemExit:825
the program doesn't exit!
Now,the game is over!
没有捕获到SystemExit异常,程序直接退出,后边的代码不执行:
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit(825)
except Exception,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
没有捕获到SystemExit异常,输出’Goodbye!'后,程序直接退出,后边的代码不执行:
song@ubuntu:~$ vi systemExit.py
song@ubuntu:~$ more systemExit.py
#!/usr/bin/python
#!coding:utf-8
import sys
if __name__=='__main__':
try:
sys.exit('Goodbye!')
except Exception,error:
print 'the information of SystemExit:{0}'.format(error)
print "the program doesn't exit!"
print 'Now,the game is over!'
song@ubuntu:~$ python systemExit.py
Goodbye!
2.os._exit():
直接退出Python解释器,不抛异常,不执行相关清理工作,其后的代码都不执行,其使用会受到平台的限制,但我们常用的Win32平台和基于UNIX的平台不会有所影响,常用在子进程的退出.一般来说os._exit()
用于在线程中退出,sys.exit()
用于在主线程中退出。
3.exit()/quit():
抛出SystemExit异常,一般在交互式shell中退出时使用。
二、virtualenv虚拟化模块
virtualenv 这个工具的问世,极大的避免了我们在开发过程中可能遇到的环境冲突。
假设当你的系统中有多个Python版本:比如:有一个2.7.5、一个2.7.12、以及一个3.2,这个时候如果是在系统内,那么,只能选择其中一个使用,而且还会有冲突的可能。
那么这个时候Virtualenv这个神器的作用来了,它可以在系统中,创建多个“独立”的Python运行环境,这个时候我们只需要给每个APP选择我们需要的Python环境就OK了。
virtualenv是如何创建“独立”的Python运行环境的呢?原理很简单,就是把系统Python复制一份到virtualenv的环境,用命令source venv/bin/activate进入一个virtualenv环境时,virtualenv会修改相关环境变量,让命令python和pip均指向当前的virtualenv环境。
下面简单介绍安装方式:
- 如果你已经安装了PIP工具可以直接执行以下命令:
pip install virtualenv
- 未安装PIP工具,老老实实的执行3个基础安装步骤:
(1)进入python官网下载virtualenv最新源码包或者使用wget 命令:wget https://pypi.python.org/packages/d4/0c/9840c08189e030873387a73b90ada981885010dd9aea134d6de30cd24cb8/virtualenv-15.1.0.tar.gz
(2)解压并进入解压目录:tar -xzvf virtualenv15.0.3.tar.gz
cd virtualenv15.0.3
(3)运行安装命令:ython setup.py install
创建一个独立的Python运行环境,命名为venv:
- 创建一个test_env分区运行下面命令:
virtualenv --no-site-packages test_env
命令virtualenv就可以创建一个独立的Python运行环境,我们还加上了参数--no-site-packages
,这样,已经安装到系统Python环境中的所有第三方包都不会复制过来,这样,我们就得到了一个不带任何第三方包的“干净”的Python运行环境。 - 进入并运行分区:
cd test_env
、source ./bin/activate
当创建分区时默认安装setuptools和pip包,版本根据你系统时使用的python版本而定
现在你可以在你的test_env这个独立环境下为所欲为了,当然例如 gcc 这些编译环境还是依赖系统环境,需要预先安装。 - 退出分区:
deactivate
补充:
python3.3以后自带venv模块支持轻量级虚拟环境,virtualenv模块仍然支持,可安装。
virtualenv --no-site-packages myvenv
等价于virtualenv myvenv
(目前新版默认不使用系统环境包)
python -m venv myvenv
也是默认全新干净的环境,相反可选的参数python -m venv --system-site-packages myvenv
使虚拟环境指向系统环境包目录(非复制),在系统环境pip新安装包,在虚拟环境就可以使用。
参考:
python virtualenv虚拟化模块介绍
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432712108300322c61f256c74803b43bfd65c6f8d0d0000#0
三、以后读写文件都使用with open语句,不要再像以前那样用f = open()这种语句了
可浏览:python文件读写,以后就用with open语句
四、os.sep的用法
python是跨平台的。在Windows上,文件的路径分隔符是 \
,在Linux上是 /
。为了让代码在不同的平台上都能运行,那么路径应该写 \
还是 /
呢?使用os.sep
的话,就不用考虑这个了,os.sep根据你所处的平台,自动采用相应的分隔符号。
举例:Linux系统某个路径 /usr/share/python
,那么上面的 os.sep
就是 /
;windows系统某个路径 C:\Users\Public\Desktop
,那么上面的 os.sep
就是\
。
data_dir = os.sep.join(['hello', 'world'])
# hello/world或者hello\world
五、os.path.realpath、os.path.join、os.path.split、os.path.dirname、os.environ.get
# 获取py脚本所在路径
print os.path.realpath(__file__)
# 输出:D:\huiq\workspace\apache-atlas-sources-2.1.0\distro\src\bin\atlas_start.py
# 使用os.path.split()分割路径与文件,以元组的形式返回,我们运用这个特性获取py文件的上级路径‘script’
print os.path.split(os.path.realpath(__file__))[0]
# 输出:D:\huiq\workspace\apache-atlas-sources-2.1.0\distro\src\bin
# print os.path.split(os.path.realpath(__file__))[0]
a=os.path.split(os.path.realpath(__file__))[0]
print(os.path.join(a,"data"))
# 输出:D:\huiq\workspace\apache-atlas-sources-2.1.0\distro\src\bin\data
# 去掉文件名,返回目录
print os.path.dirname(os.path.realpath(__file__))
# 输出:D:\huiq\workspace\apache-atlas-sources-2.1.0\distro\src\bin
# 获取环境变量的一个方法
print os.environ.get("HADOOP_HOME")
# 输出:C:\Users\9\Desktop\hadoop-2.6.0
# 如果该变量存在则返回该变量的值,如果该变量不存在则返回第二个参数的值
print os.environ.get("HADOOP_HOME", "adfs")
print os.environ.get("HADOOP_HOMEE", "adfs")
# 输出:
# C:\Users\9\Desktop\hadoop-2.6.0
# adfs