• 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 */
  • 相关阅读:
    RHEL因为selinux设置失误,无法重启问题。(centos适用)
    Linux系统忘记管理员密码(CentOS、RHEL、Ubuntu)
    cobbler PXE 安装系统时出现的问题
    虚拟机ping 不通主机,主机可ping 虚拟机解决方法
    CentOS7系统更改网卡名为eth0
    PXE-cobbler 无人值守装机------续
    PXE-cobbler 无人值守装机
    DELL PowerEdge R410系统日志满报错
    matlab添加toolbox失败的解决办法
    vs2017+opencv3.4.0的配置方法
  • 原文地址:https://www.cnblogs.com/frankielf0921/p/5834292.html
Copyright © 2020-2023  润新知