Python学习入门笔记(一):Python文件类型

1、源代码

   扩展名:.py,由Python程序解释,不需要编译。

--创建hello.py源文件

1
2
# cat hello.py 
print 'Hello World!'

--执行hello.py

1
2
3
4
5
[root@XjTest study]# chmod a+x hello.py 
[root@XjTest study]# python hello.py 
Hello World!
[root@XjTest study]# ./hello.py 
./hello.py: line 1print: command not found

备注:./hello.py方式不能执行Python文件,原因:没有指定Python解析器。

1
2
3
4
5
[root@XjTest study]# cat hello.py 
#!/usr/bin/python
print 'Hello World!'
[root@XjTest study]# ./hello.py 
Hello World!


2、字节代码

   扩展名:.pyc,由Python源文件经编译后生成的。

--生成hello.pyc

1
2
3
4
5
6
7
8
9
10
11
[root@XjTest study]# python
Python 2.6.6 (r266:84292, Sep  4 201307:46:00
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help""copyright""credits" or "license" for more information.
>>> import py_compile
>>> py_compile.compile('hello.py')
>>> exit()
[root@XjTest study]# ll
总用量 8
-rwxr-x--1 root root  39 7月   6 11:47 hello.py
-rw-r----- 1 root root 117 7月   6 11:50 hello.pyc

--执行

1
2
[root@XjTest study]# python hello.pyc
Hello World!


3、优化代码


    扩展名:.pyo,经过优化的源文件。

--生成hello.pyo

1
2
3
4
5
6
# python -O -m py_compile hello.py
[root@XjTest study]# ll
总用量 12
-rwxr-x--1 root root  39 7月   6 11:47 hello.py
-rwxr-x--1 root root 117 7月   6 11:50 hello.pyc
-rwxr-x--- 1 root root 117 7月   6 11:56 hello.pyo

--执行

1
2
[root@XjTest study]# python hello.pyo
Hello World!






1、源代码

   扩展名:.py,由Python程序解释,不需要编译。

--创建hello.py源文件

1
2
# cat hello.py 
print 'Hello World!'

--执行hello.py

1
2
3
4
5
[root@XjTest study]# chmod a+x hello.py 
[root@XjTest study]# python hello.py 
Hello World!
[root@XjTest study]# ./hello.py 
./hello.py: line 1print: command not found

备注:./hello.py方式不能执行Python文件,原因:没有指定Python解析器。

1
2
3
4
5
[root@XjTest study]# cat hello.py 
#!/usr/bin/python
print 'Hello World!'
[root@XjTest study]# ./hello.py 
Hello World!


2、字节代码

   扩展名:.pyc,由Python源文件经编译后生成的。

--生成hello.pyc

1
2
3
4
5
6
7
8
9
10
11
[root@XjTest study]# python
Python 2.6.6 (r266:84292, Sep  4 201307:46:00
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help""copyright""credits" or "license" for more information.
>>> import py_compile
>>> py_compile.compile('hello.py')
>>> exit()
[root@XjTest study]# ll
总用量 8
-rwxr-x--1 root root  39 7月   6 11:47 hello.py
-rw-r----- 1 root root 117 7月   6 11:50 hello.pyc

--执行

1
2
[root@XjTest study]# python hello.pyc
Hello World!


3、优化代码

    扩展名:.pyo,经过优化的源文件。

--生成hello.pyo

1
2
3
4
5
6
# python -O -m py_compile hello.py
[root@XjTest study]# ll
总用量 12
-rwxr-x--1 root root  39 7月   6 11:47 hello.py
-rwxr-x--1 root root 117 7月   6 11:50 hello.pyc
-rwxr-x--- 1 root root 117 7月   6 11:56 hello.pyo

--执行

1
2
[root@XjTest study]# python hello.pyo
Hello World!




     本文转自stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1434985,如需转载请自行联系原作者





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值