python学习系列之OS模块介绍
2012-02-03 19:25
1126 查看
今天我们来介绍的是python的一个非常强大的模块---OS,我们来示例的时候不是用的标准的python,而是用的python的同胞兄弟Ipython,ipython
是一个 python 的交互式 shell,比默认的 python shell 好用得多,支持变量自动补全,自动缩近,支持 bash shell 命令,内置了许多很有用的功能和函数。
Ipython是需要单独安装的,安装起来非常简单,如下:
wget http://ipython.scipy.org/dist/0.10.1/ipython-0.10.1.zip
unzip ipython-0.10.1.zip
cd ipython-0.10.1
ls
python setup.py install
支持Ipython安装完毕。
IPython与标准Python的最大区别在于,Ipython会对命令提示符的每一行进行编号。下面我们来用ipython来学习下OS的功能:
[root@localhost ~]# ipython
Python 2.6 (r26:66714, Dec 17
2010, 11:17:00)
Type "copyright", "credits"
or
"license"
for more information.
IPython 0.10.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: import os
导入OS模块
In [2]: os.getcw
os.getcwd os.getcwdu
按了一下TAB键,自动为你对齐和列举。这个是懒人最喜欢的
In [2]: os.getcwd() 得到当前的路径
Out[2]: '/root'
In [3]: os.chdir("/et") 更换目录和路径
Display all 244 possibilities? (y or n) 自动列举
In [3]: os.chdir("/etc/init.") 习惯性的手按了TAB,哈哈。。。这里都能帮我们自动对齐,强大吧
/etc/init.d/NetworkManager /etc/init.d/mysqld
/etc/init.d/acpid /etc/init.d/nagios
/etc/init.d/anacron /etc/init.d/netconsole
/etc/init.d/apmd /etc/init.d/netfs
。。。
--More--
In [3]: os.chdir("/etc/init.d/") 更换路径到/etc/init.d下
In [4]: os.getcwd() 我们来看看当前的路径
Out[4]: '/etc/rc.d/init.d' 输出显示是我们切换的路径
In [5]: os.ctermid() 返回文件名对应名对应的进程的控制终端
Out[5]: '/dev/tty'
In [6]: os.getegid() 得到当前进程的ID
Out[6]: 0
In [7]: os.getg Tab键自动对齐
os.getgid os.getgroups
In [7]: os.getgid() 得到当前进程的ID
Out[7]: 0
In [8]: os.getgroups() 得到补充组列表的ID和当前进程
Out[8]: [0, 1, 2, 3, 4, 6, 10]
In [9]: os.getlogin() 得到当前控制终端的登录用户名
Out[9]: 'root'
In [10]: os.getpgrp() 得到当前进程组ID
Out[10]: 5484
In [11]: os.getp
os.getpgid os.getpgrp os.getpid os.getppid
In [11]: os.getpid() 得到当前进程ID
Out[11]: 5484
In [12]: os.getppid() 得到父进程ID
Out[12]: 5456
In [13]: os.getuid() 得到当前的用户ID
Out[13]: 0
In [14]: os.uname() 得到当前运行的版本详细信息
Out[14]:
('Linux',
'localhost.localdomain',
'2.6.18-194.26.1.el5',
'#1 SMP Tue Nov 9 12:54:40 EST 2010',
'i686')
In [15]: os.name()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/etc/rc.d/init.d/<ipython console> in <module>()
TypeError: 'str' object is
not callable
In [16]: os.name 得到操作系统,如果是window下的话,得到的返回值应该是"NT"
Out[16]: 'posix'
以上介绍的都是比较简单和常用的功能,大家可以继续研究更强大的功能。。。。
此文亦在我的51CTO博客(world77.blog.51cot.com)上有发表,如有纠纷,请告知
是一个 python 的交互式 shell,比默认的 python shell 好用得多,支持变量自动补全,自动缩近,支持 bash shell 命令,内置了许多很有用的功能和函数。
Ipython是需要单独安装的,安装起来非常简单,如下:
wget http://ipython.scipy.org/dist/0.10.1/ipython-0.10.1.zip
unzip ipython-0.10.1.zip
cd ipython-0.10.1
ls
python setup.py install
支持Ipython安装完毕。
IPython与标准Python的最大区别在于,Ipython会对命令提示符的每一行进行编号。下面我们来用ipython来学习下OS的功能:
[root@localhost ~]# ipython
Python 2.6 (r26:66714, Dec 17
2010, 11:17:00)
Type "copyright", "credits"
or
"license"
for more information.
IPython 0.10.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: import os
导入OS模块
In [2]: os.getcw
os.getcwd os.getcwdu
按了一下TAB键,自动为你对齐和列举。这个是懒人最喜欢的
In [2]: os.getcwd() 得到当前的路径
Out[2]: '/root'
In [3]: os.chdir("/et") 更换目录和路径
Display all 244 possibilities? (y or n) 自动列举
In [3]: os.chdir("/etc/init.") 习惯性的手按了TAB,哈哈。。。这里都能帮我们自动对齐,强大吧
/etc/init.d/NetworkManager /etc/init.d/mysqld
/etc/init.d/acpid /etc/init.d/nagios
/etc/init.d/anacron /etc/init.d/netconsole
/etc/init.d/apmd /etc/init.d/netfs
。。。
--More--
In [3]: os.chdir("/etc/init.d/") 更换路径到/etc/init.d下
In [4]: os.getcwd() 我们来看看当前的路径
Out[4]: '/etc/rc.d/init.d' 输出显示是我们切换的路径
In [5]: os.ctermid() 返回文件名对应名对应的进程的控制终端
Out[5]: '/dev/tty'
In [6]: os.getegid() 得到当前进程的ID
Out[6]: 0
In [7]: os.getg Tab键自动对齐
os.getgid os.getgroups
In [7]: os.getgid() 得到当前进程的ID
Out[7]: 0
In [8]: os.getgroups() 得到补充组列表的ID和当前进程
Out[8]: [0, 1, 2, 3, 4, 6, 10]
In [9]: os.getlogin() 得到当前控制终端的登录用户名
Out[9]: 'root'
In [10]: os.getpgrp() 得到当前进程组ID
Out[10]: 5484
In [11]: os.getp
os.getpgid os.getpgrp os.getpid os.getppid
In [11]: os.getpid() 得到当前进程ID
Out[11]: 5484
In [12]: os.getppid() 得到父进程ID
Out[12]: 5456
In [13]: os.getuid() 得到当前的用户ID
Out[13]: 0
In [14]: os.uname() 得到当前运行的版本详细信息
Out[14]:
('Linux',
'localhost.localdomain',
'2.6.18-194.26.1.el5',
'#1 SMP Tue Nov 9 12:54:40 EST 2010',
'i686')
In [15]: os.name()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/etc/rc.d/init.d/<ipython console> in <module>()
TypeError: 'str' object is
not callable
In [16]: os.name 得到操作系统,如果是window下的话,得到的返回值应该是"NT"
Out[16]: 'posix'
以上介绍的都是比较简单和常用的功能,大家可以继续研究更强大的功能。。。。
此文亦在我的51CTO博客(world77.blog.51cot.com)上有发表,如有纠纷,请告知
相关文章推荐
- python系列之os模块学习
- Python 模块学习:os模块
- Python 模块学习:os模块
- python学习笔记1——os模块
- Python模块学习系列(7)----copy模块
- 学习python的第三十六天-OS模块,特殊的方法
- python 学习笔记 os模块
- python学习—Day20—模块:logging、os、command、sys
- python标准库os模块学习
- Python 模块学习:os模块
- Python之os模块的进程管理介绍
- Python学习笔记(Python OS模块)
- python 模块学习_os
- Python 学习File、os模块操作文件
- python os模块详细介绍
- 深度学习---Python OS模块命令
- Python 模块学习:os模块
- python os.path模块学习
- Python 模块学习:os模块
- Python学习笔记之os模块