python文件类型

/* 1. python 源码文件以"py"为扩展名 */
[root@localhost ~]# mkdir test1
[root@localhost ~]# cd test1/
[root@localhost test1]# vi 1.py
//ADD
#!/usr/bin/python

print 'hello world'

[root@localhost test1]# python 1.py
hello world
[root@localhost test1]# ls -l
总用量 4
-rw-r--r--. 1 root root 39 9月   3 01:22 1.py

/* 加执行权限后,可以直接执行 */
[root@localhost test1]# chmod +x 1.py
[root@localhost test1]# ls -l
总用量 4
-rwxr-xr-x. 1 root root 39 9月   3 01:22 1.py
[root@localhost test1]# ./1.py
hello world

 

/* 2. 源码经过编译后生成 扩展名为 "pyc" 的文件 */
[root@localhost test1]# vi 2.py
//ADD
#!/usr/bin/python

import py_compile

py_compile.compile('1.py')

//执行后,没有反应。但是产生了 1.pyc --〉对1.py的字符编译
//1.pyc -〉 二进制 ,无法看懂
[root@localhost test1]# python 2.py
[root@localhost test1]# ls
1.py  1.pyc  2.py
[root@localhost test1]# file 1.pyc
1.pyc: python 2.6 byte-compiled
[root@localhost test1]# python 1.pyc
hello world

//如果把源码移走了,编译后的文件还是可以被执行
[root@localhost test1]# mv 1.py /tmp/
[root@localhost test1]# ls
1.pyc  2.py
[root@localhost test1]# python 1.pyc
hello world

 

/* 3.经过优化的源码文件,扩展名为"pyo" */

//先把文件移回来
[root@localhost test1]# mv /tmp/1.py .
[root@localhost test1]# ls
1.py  1.pyc  2.py

//test
[root@localhost test1]# python -O -m py_compile 1.py
[root@localhost test1]# ls
1.py  1.pyc  1.pyo  2.py
[root@localhost test1]# ls -l
总用量 16
-rwxr-xr-x. 1 root root  39 9月   3 01:22 1.py
-rw-r--r--. 1 root root 112 9月   3 01:27 1.pyc
-rw-r--r--. 1 root root 112 9月   3 01:32 1.pyo
-rw-r--r--. 1 root root  65 9月   3 01:27 2.py
[root@localhost test1]# python 1.pyo
hello world

 

/* 如果想把一个源码写好后,不想要别人看,则可以将它编译成pyo 或 pyc */

 

posted @ 2016-09-02 17:34  Frankiee  阅读(270)  评论(0编辑  收藏  举报