http://blog.csdn.net/pipisorry/article/details/42167683
Python的系统模块
包括sys, os, glob, socket, threading, _thread, queue, time, timeit, subprocess, multiprocessing, signal, select, shutil, tempfile等。
大多数系统级接口集中在:sys和os两个模块。
sys模块包含:
平台与版本的信息,如sys.platform, sys.maxsize, sys.version
模块搜索路径sys.path
模块表sys.modules,这是一个包含python程序中import进来的模块的name:module信息的字典
异常信息,如sys.exc_info()
命令行参数sys.argv
标准流,如sys.stdin, sys.stdout, sys.stderr
[python使用字符串构建stdin对象]
程序退出调用sys.exit
sys模块
sys模块可以参看python文档 http://docs.python.org/library/sys.html。
变量sys.argv
sys.argv: 实现从程序外部向程序传递参数。
示例:
print.py:
import sys
print sys.argv[0]
print sys.argv[1]
print sys.argv[2]
>>>python print.py arg1 arg2
Note:
1. 脚本的名称总是sys.argv
列表的第一个参数。所以,在这里,print.py是sys.argv[0]
、arg1
是sys.argv[1]
。
2. 一般要加入if len(sys.argv) > 1:来判断是否有参数传入
实现从程序内部向程序传递参数,完成从外部传入一样的功能
sys.argv='a.py --b c --d e f'.split()
或者
sys.argv += ['--token_data', '../../data/tokenized_dir', '--do_train', '--do_decode']
其它变量
sys.path:包含输入模块的目录名列表。sys.path
的第一个字符串是空的——这个空的字符串表示当前目录也是sys.path
的一部分,这与PYTHONPATH
环境变量是相同的。这意味着你可以直接输入位于当前目录的模块。否则,你得把你的模块放在sys.path
所列的目录之一。
sys.platform: 获取当前系统平台。
方法sys.setrecursionlimit
python默认的递归深度是很有限的,大概是900多的样子(平台相关),当递归深度超过这个值的时候,就会引发这样的一个异常:RuntimeError: maximum recursion depth exceeded。
手工设置递归调用深度
import sys
sys.setrecursionlimit(6000)
def rec(i):
i += 1
if not i % 500:
print(i)
return rec(i)
rec(i)
其它方法
sys.exit([arg]): 程序中间的退出,arg=0为正常退出。
sys.getdefaultencoding(): 获取系统当前编码,一般默认为ascii。
sys.setdefaultencoding(): 设置系统默认编码,执行dir(sys)时不会看到这个方法,在解释器中执行不通过,可以先执行reload(sys),在执行setdefaultencoding('utf8'),此时将系统默认编码设置为utf8。(见设置系统默认编码 )
sys.getfilesystemencoding(): 获取文件系统使用编码方式,Windows下返回'mbcs',mac下返回'utf-8'.
[sys — System-specific parameters and functions]
SYS系统模块的应用
Python 搜索路径
使用 import 语句时,Python 所查找的系统目录清单。
举例来说,假定你将 Python 路径设置为 ['','/usr/lib/python2.4/site-packages','/home/username/djcode/'] 。如果执行代码fromfooimportbar ,Python 将会首先在当前目录查找foo.py 模块( Python 路径第一项的空字符串表示当前目录)。 如果文件不存在,Python将查找/usr/lib/python2.4/site-packages/foo.py 文件。
如果你想看Python搜索路径的值,运行Python交互解释器,然后输入:
>>> import sys
>>> print sys.path
通常,你不必关心 Python 搜索路径的设置。 Python 和 Django 会在后台自动帮你处理好。
python代码暂停执行的几种方法
1.time.sleep(secs)
(程序真正暂停的时间可能长于请求的时间也可能短于暂停的时间)
Suspend execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.
2. raw_input( )
通过等待输入来让程序暂停
3. os.system("pause")
通过执行操作系统的命令来让程序暂停,该函数是通过实现标准C函数system( )来实现的。
4.subprocess.call
Python2.4新加入了subprocess模块,而且官方建议使用该模块替换os.system所以,也可以这样写:subprocess.call("pause",shell=True)
python终止程序运行
让python程序自动退出:exit()
程序陷入死循环,强制结束:Ctrl + C
from:python模块-系统模块sys_-柚子皮-的博客-CSDN博客
ref:http://www.cnpythoner.com/post/89.html
[操作系统服务:OS模块 ]