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  1 print : 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  2013 07 : 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  1 print : 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  2013 07 : 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,如需转载请自行联系原作者





相关文章
|
23小时前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
1天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
1天前
|
存储 索引 Python
Python从入门到精通——1.3.1练习编写简单程序
Python从入门到精通——1.3.1练习编写简单程序
|
1天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
1天前
|
数据采集 数据可视化 数据处理
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
|
1天前
|
Java 数据库连接 数据处理
Python从入门到精通:3.1.2多线程与多进程编程
Python从入门到精通:3.1.2多线程与多进程编程
|
1天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
9天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
49 0
|
10天前
|
Python
python学习10-函数
python学习10-函数
|
10天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环

热门文章

最新文章