os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cdos.curdir 返回当前目录: (‘.‘)os.pardir 获取当前目录的父目录字符串名:(‘..‘)os.makedirs(‘dirname1/dirname2‘) 可生成多层递归目录os.removedirs(‘dirname1‘) 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推os.mkdir(‘dirname‘) 生成单级目录;相当于shell中mkdir dirnameos.rmdir(‘dirname‘) 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirnameos.listdir(‘dirname‘) 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印os.remove() 删除一个文件os.rename("oldname","newname") 重命名文件/目录os.stat(‘path/filename‘) 获取文件/目录信息os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"os.pathsep 输出用于分割文件路径的字符串os.name 输出字符串指示当前使用平台。win->‘nt‘; Linux->‘posix‘os.system("bash command") 运行shell命令,直接显示os.environ 获取系统环境变量os.path.abspath(path) 返回path规范化的绝对路径os.path.split(path) 将path分割成目录和文件名二元组返回os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素os.path.exists(path) 如果path存在,返回True;如果path不存在,返回Falseos.path.isabs(path) 如果path是绝对路径,返回Trueos.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回Falseos.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回Falseos.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间asctime([tuple]) -> string 将元组格式转换成字符串格式
clock() -> floating point number 在win下为当前时间,linux下为程序占用CPU的时间
ctime(seconds) -> string 默认为当前UTC-8时间戳的字符串 , 如果有参数是1970-1-1 开始加secnds的时间戳
gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,tm_sec, tm_wday,tm_yday,tm_isdst) 默认当前UTC时间的元组,isdst是否是夏令时
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst) 默认当前U本地UTC-8时间的元组,isdst是否是夏令时
mktime(tuple) -> floating point number 与gmtime()和localtime()操作相反
sleep(seconds) 休眠多少秒
strftime(format[, tuple]) -> string 格式输出的字符串时间
print time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t))
strptime(string, format) -> struct_time 将格式化的时间,转换成元组格式
time() -> floating point number 把当前时间格式变成1970到现在的秒数

date(year, month, day) --> date object
ctime(...)
fromordinal(...)
fromtimestamp(...)
isocalendar(...)
isoformat(...)
isoweekday(...)
replace(...)
strftime(...)
timetuple(...)
today(...)
toordinal(...)
weekday(...)
max = datetime.date(9999, 12, 31) min = datetime.date(1, 1, 1) resolution = datetime.timedelta(1)
datetime.datetime.now() 时间的元组== datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
datetime.datetime.now() + datetime.timedelta(+/- 数字)
astimezone(...)
combine(...)
ctime(...)
date(...)
dst(...)
fromtimestamp(...)
isoformat(...)
now(...)
replace(...)
strptime(...)
time(...)
timetuple(...)
timetz(...)
tzname(...)
utcfromtimestamp(...)
utcnow(...)
utcoffset(...)
utctimetuple(...)
fromordinal(...)
isocalendar(...)
isoweekday(...)
strftime(...)
today(...)
toordinal(...)
weekday(...)
dst(...)
isoformat(...)
replace(...)
strftime(...)
tzname(...)
utcoffset(...)
random.seed(a=None, version=2)初始化随机数发生器,如果a被忽略或者为None,则使用系统当前时间。这个用于把一个随机数初始化
random.randrange(start, stop, [, step]) ,产生一个随机整数,不包括stop
randint(a,b) ,返回一个整数,包括a,b
random.choice(seq) 从一个非空序列随机返回一个元素,空报错
random.shuffle == ? radom.random() 返回0-1之间的浮点数
random.sample([1,2,3,4,5,67,],2) 从序列或Set中随机选择k个,组成唯一元素的列表
random.uniform(a,b) 返回一个a,到b 之间的浮点数
random.triangular(low, high, mode) 返回一个默认是0-1之间的,mode默认为对称分布的值
#!/usr/bin/env python#_*_encoding: utf-8_*_import randomprint (random.random()) #0.6445010863311293 #random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0print (random.randint(1,7)) #4#random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。# 其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= bprint (random.randrange(1,10)) #5#random.randrange的函数原型为:random.randrange([start], stop[, step]),# 从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),# 结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。# random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。print(random.choice(‘liukuni‘)) #i#random.choice从序列中获取一个随机元素。# 其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。# 这里要说明一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。# list, tuple, 字符串都属于sequence。有关sequence可以查看python手册数据模型这一章。# 下面是使用choice的一些例子:print(random.choice("学习Python"))#学print(random.choice(["JGood","is","a","handsome","boy"])) #Listprint(random.choice(("Tuple","List","Dict"))) #Listprint(random.sample([1,2,3,4,5],3)) #[1, 2, 5]#random.sample的函数原型为:random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。
#!/usr/bin/env python# encoding: utf-8import randomimport string#随机整数:print( random.randint(0,99)) #70#随机选取0到100间的偶数:print(random.randrange(0, 101, 2)) #4#随机浮点数:print( random.random()) #0.2746445568079129print(random.uniform(1, 10)) #9.887001463194844#随机字符:print(random.choice(‘abcdefg&#%^*f‘)) #f#多个字符中选取特定数量的字符:print(random.sample(‘abcdefghij‘,3)) #[‘f‘, ‘h‘, ‘d‘]#随机选取字符串:print( random.choice ( [‘apple‘, ‘pear‘, ‘peach‘, ‘orange‘, ‘lemon‘] )) #apple#洗牌#items = [1,2,3,4,5,6,7]print(items) #[1, 2, 3, 4, 5, 6, 7]random.shuffle(items)print(items) #[1, 4, 7, 2, 5, 3, 6]import randomcheckcode = ‘‘for i in range(4): current = random.randrange(0,4) if current != i: temp = chr(random.randint(65,90)) else: temp = random.randint(0,9) checkcode += str(temp)print (checkcode)sys.argv 返回参数的列表,第一个为函数名
sys.exit(n) 退出程序,正常退出为sys.exit(0)
sys.version 获取解释器的版本
sys.maxint 最的int值
sys.path python解释器额度环境变量
sys.platform 返回操作系统平台名称
sys.stdout.write(‘please:‘) 向屏幕输出。。
val = sys.stdin.readline()[:-1] 将标准输入保存到val中
sys.stdin/stdout/stderr 默认行为文件,可以write,read操作
原文:http://www.cnblogs.com/alen-z/p/7589355.html