python的源代码文件格式_初识Python-文件类型(入坑篇)

[root@master python]# vim hello.py

#!/usr/bin/python

print 'hello world'

语法:python【file.py】

##执行一个Python文件,通过命令Python file-name执行文件

[root@master python]# python hello.py

hello world

二、字节代码【.pyc】

##通常我们写好的源代码文件.py是可以看到里面的代码内容,如果不想让别人看到里面的源码,可以通过import插入py_compile模块来对源码文件进行编译后,内容变为二进制字符无法查看源码内容,经过编译后会生成一个.pyc的Python文件。

py_compile.compile('源码文件')

[root@master python]# vim 2.py

#!/usr/bin/python

importpy_compile

py_compile.compile('hello.py')

执行文件后,可以看到在源码的基础上,编译出一个新的.pyc文件,源代码文件不存在的情况下,编译过后的代码文件依旧可以执行显示正常的输出信息!!

[root@master python]# python 2.py

[root@master python]# ls

2.py hello.py hello.pyc

[root@master python]# python hello.pyc

hello world

经过编译后的文件内容为二进制字节码,而且类型也发生变化为bety文件

[root@master python]# vim hello.pyc

^Có

y^N^F^c^@^@^@^@^@^@^@^@^A^@^@^@@^@^@^@s ^@^@^@d^@^@GHd^A^@S(^B^@^@^@s ^@^@^@hell wordN(^@^@^@^@(^@^@^@^@(^@^@^@^@(^@^@^@^@s^G^@^@^@hell.pyt^H^@^@^@^C^@^@^@s^@^@^@^@

[root@master python]# file hello.pyc

hello.pyc: python 2.7 byte-compiled

三、优化代码【.pyo】

##.pyo是优化编译后的文件,同样编译过后的文件内容也是二进制字节码,通过-O表示优化,-m指定调用py_compile 模块执行源码文件。

语法:python -O -m py_compile【源代码】

[root@master python]# python -O -m py_compile hello.py

[root@master python]# ls

2.py hello.py hello.pyc hello.pyo

[root@master python]# python hello.pyo

hello world

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值