您的位置:首页 > 编程语言 > Python开发

Python语法备忘

2016-01-14 15:09 453 查看
1. Python 字典(Dictionary) fromkeys()方法

描述

Python 字典(Dictionary) fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。

语法

fromkeys()方法语法:

dict.fromkeys(seq[, value]))
参数

seq -- 字典键值列表。
value -- 可选参数, 设置键序列(seq)的值。
实例

seq = ('name', 'age', 'sex')

dict = dict.fromkeys(seq)
print "New Dictionary : %s" % str(dict)

dict = dict.fromkeys(seq, 10)
print "New Dictionary : %s" % str(dict)
输出

New Dictionary : {'age': None, 'name': None, 'sex': None}
New Dictionary : {'age': 10, 'name': 10, 'sex': 10}

2. 返回python.exe文件路径

sys.executable
A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.
实例

python_exe_file = sys.executable.replace('\\', '/')
print python_exe_file
py_test_exe_file = os.path.abspath(os.path.join(os.path.split(sys.executable)[0], 'Scripts/py.test.exe')).replace('\\', '/')
print py_test_exe_file
输出

C:/Tools/Python27/python.exe
C:/Tools/Python27/Scripts/py.test.exe

3. 切换工作目录

os.chdir(path)
Change the current working directory to path.
Availability: Unix, Windows.

4. 自动编译并将报告输出

cmd中执行:
C:\Resources\Git\FreeMV>"c:\Tools\IAR Systems\Embedded Workbench 7.3\common\bin\IarBuild.exe" "c:\Tools\Kinetis_Bootloader_2_0_0_d1\targets\MK64F12\iar\tower_bootloader\tower_bootloader.ewp" -build "Debug" -log all >> c:\Resources\Git\iar_ide.log 2>&1

说明: >, >> 的作用是导出执行文件的内容到指定位置,一般是加在执行命令的后面就可以导出的,> 是覆盖,>> 是追加
1  :表示stdout标准输出,系统默认值是1,所以">/dev/null"等同于"1>/dev/null"
2  :表示stderr标准错误
&  :表示等同于的意思
在shell中,每个进程都和三个系统文件 相关联:标准输入stdin,标准输出stdout、标准错误stderr,三个系统文件的文件描述符分别为0,1、2。所以这里2>&1 的意思就是将标准错误也输出到标准输出当中。

command >out.file 2>&1 &
command >out.file是将command的输出重定向到out.file文件,即输出内容不打印到屏幕上,而是输出到out.file文件中。 2>&1 是将标准出错重定向到标准输出,这里的标准输出已经重定向到了out.file文件,即将标准出错也输出到out.file文件中。最后一个& , 是让该命令在后台执行。
 http://blog.cs 4000
dn.net/gzh0222/article/details/9351067

5. threading Thread.join用法
 http://zipperary.com/2013/07/28/python-thread-join/ 
import threading

import time

def doWaiting1():

print 'start waiting1: ' + time.strftime('%H:%M:%S')

time.sleep(3)

print 'stop waiting1: ' + time.strftime('%H:%M:%S')

def doWaiting2():

print 'start waiting2: ' + time.strftime('%H:%M:%S')

time.sleep(8)

print 'stop waiting2: ' + time.strftime('%H:%M:%S')

tsk = []

thread1 = threading.Thread(target = doWaiting1)

thread1.start()

tsk.append(thread1)

thread2 = threading.Thread(target = doWaiting2)

thread2.start()

tsk.append(thread2)

# print tsk

print 'start join: ' + time.strftime('%H:%M:%S')

for tt in tsk:

tt.join()

tt.join(2)

print 'end join: ' + time.strftime('%H:%M:%S')

tt.join()

start waiting1: 18:45:18

start waiting2: 18:45:18

start join: 18:45:18

stop waiting1: 18:45:21

stop waiting2: 18:45:26

end join: 18:45:26

tt.join(2)
start waiting1: 18:38:32
start waiting2: 18:38:32
start join: 18:38:32
stop waiting1: 18:38:35
end join: 18:38:36
stop waiting2: 18:38:40

两个线程开始并发执行,然后执行线程1的join(2),等线程1执行2s后就不管它了,执行线程2的join(2),等线程2执行2s后也不管它了(在此过程中线程1执行结束,打印线程1的结束信息),开始执行主进程,打印「end join」。4s之后线程2执行结束。

6. cmd 命令

unzip -o -d /home/sunny myfile.zip
把myfile.zip文件解压到 /home/sunny/
-o:不提示的情况下覆盖文件;
-d:-d /home/sunny 指明将文件解压缩到/home/sunny目录下;
二、完整语法
unzip [-cflptuvz] [-agCjLMnoqsVX] [-P <密码>] [.zip文件] [文件] [-d <目录>] [-x <文件>] 或 unzip [-Z]

三、参数列表
-c :将解压缩的结果显示到屏幕上,并对字符做适当的转换。
-f :更新现有的文件。
-l :显示压缩文件内所包含的文件。
-p :与-c参数类似,会将解压缩的结果显示到屏幕上,但不会执行任何的转换。
-t :检查压缩文件是否正确。
-u :与-f参数类似,但是除了更新现有的文件外,也会将压缩文件中的其他文件解压缩到目录中。
-v :执行是时显示详细的信息。
-z :仅显示压缩文件的备注文字。
-a :对文本文件进行必要的字符转换。
-b :不要对文本文件进行字符转换。
-C :压缩文件中的文件名称区分大小写。
-j :不处理压缩文件中原有的目录路径。
-L :将压缩文件中的全部文件名改为小写。
-M :将输出结果送到more程序处理。
-n :解压缩时不要覆盖原有的文件。
-o :不必先询问用户,unzip执行后覆盖原有文件。
-P<密码> :使用zip的密码选项。
-q :执行时不显示任何信息。
-s :将文件名中的空白字符转换为底线字符。
-V :保留VMS的文件版本信息。
-X :解压缩时同时回存文件原来的UID/GID。
[.zip文件] :指定.zip压缩文件。
[文件] :指定要处理.zip压缩文件中的哪些文件。
-d<目录> :指定文件解压缩后所要存储的目录。
-x<文件> :指定不要处理.zip压缩文件中的哪些文件。
-Z unzip :-Z等于执行zipinfo指令。 http://racoguo.blog.51cto.com/2309068/1288050 
7. logging

日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL。
DEBUG:详细的信息,通常只出现在诊断问题上
INFO:确认一切按预期运行
WARNING:一个迹象表明,一些意想不到的事情发生了,或表明一些问题在不久的将来(例如。磁盘空间低”)。这个软件还能按预期工作。
ERROR:更严重的问题,软件没能执行一些功能
CRITICAL:一个严重的错误,这表明程序本身可能无法继续运行
这5个等级,也分别对应5种打日志的方法: debug 、info 、warning 、error 、critical。默认的是WARNING,当在WARNING或之上时才被跟踪 http://blog.csdn.net/liuchunming033/article/details/39080457 
8. os.walk用法

os.walk(top, topdown=True, onerror=None, followlinks=False)

可以得到一个三元tupple(dirpath, dirnames, filenames),
第一个为起始路径,第二个为起始路径下的文件夹,第三个是起始路径下的文件。
dirpath 是一个string,代表目录的路径,
dirnames 是一个list,包含了dirpath下所有子目录的名字。
filenames 是一个list,包含了非目录文件的名字。
这些名字不包含路径信息,如果需要得到全路径,需要使用os.path.join(dirpath, name).
通过for循环自动完成递归枚举
 http://blog.csdn.net/bagboy_taobao_com/article/details/8938126[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python