Python模块 os commands
os模块
在自动化运维和测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,对于python而言这就需要依赖于os模块。下面就学习下os模块常用的几个方法。
>>> import os #导入os模块
>>> help(os) #查看os模块帮助文档,里面详细的模块相关函数和使用方法
>>> dir(os) #查看os模块所支持的方法
import os
print dir(os)
['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list', '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fstat', 'fsync', 'getcwd', 'getcwdu', 'getenv', 'getpid', 'isatty', 'kill', 'linesep', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys', 'system', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'walk', 'write']
(1)os.name获取当前正在使用的平台,Windows 返回 ‘nt‘; Linux 返回’posix‘
#Linux
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.name
'posix'
>>>
#Windows
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.name
'nt'
>>>
(2)os.system(command)执行shell命令
#Windows
>>> os.system('netstat -an |findstr 8080')
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING
TCP 192.168.31.37:6959 183.192.196.205:8080 CLOSE_WAIT
TCP [::]:8080 [::]:0 LISTENING
#Linux
>>> os.system('ip addr list')
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 66:95:73:bf:5f:97 brd ff:ff:ff:ff:ff:ff
inet 218.207.221.92/27 brd 218.207.221.95 scope global eth0
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether b2:91:14:7e:78:ae brd ff:ff:ff:ff:ff:ff
0
(3)当前路径及路径下的文件
os.getcwd():查看当前所在路径
os.listdir(path):列举目录下的所有文件。返回的是列表类型
os.path.abspath(path):返回path的绝对路径
import os
print os.getcwd()
print os.listdir(os.getcwd())
print os.path.abspath('.')
print os.path.abspath('..')
运行结果:
C:\Users\YangQing\PycharmProjects\Test\modules\main
['client.py', 'M_os.py', 'rizhi.py', 'shijian.py', 'test.log', '__init__.py', '__init__.pyc']
C:\Users\YangQing\PycharmProjects\Test\modules\main
C:\Users\YangQing\PycharmProjects\Test\modules
(4)查看路径的文件夹部分和文件名部分
os.path.split(path):将路径分解为(文件夹,文件名),返回的是元组类型
os.path.join(path1,path2,...):将path进行组合,若其中有绝对路径,则之前的path将被删除
os.path.dirname(path):返回path中的文件夹部分,结果不包含'\'
os.path.basename(path):返回path中的文件名。
import os
print os.path.split('.')
print os.path.split('C:\Users\YangQing\PycharmProjects\Test\modules\main\test.log')
print os.path.split('C:\Users\YangQing\PycharmProjects\Test\modules\main\\')
print os.path.split('C:\Users\YangQing\PycharmProjects\Test\modules\main')
print os.path.join('C:\Users\YangQing\PycharmProjects\Test\modules','main')
print os.path.join('C:\Users\YangQing\PycharmProjects\Test\modules\main','test.log')
print os.path.join('C:\Users\YangQing\PycharmProjects\Test\modules\main','C:\Users\YangQing\PycharmProjects\Test')
print os.path.dirname('C:\Users\YangQing\PycharmProjects\Test')
print os.path.basename('C:\Users\YangQing\PycharmProjects\Test')
运行结果:
('', '.')
('C:\\Users\\YangQing\\PycharmProjects\\Test\\modules', 'main\test.log')
('C:\\Users\\YangQing\\PycharmProjects\\Test\\modules\\main', '')
('C:\\Users\\YangQing\\PycharmProjects\\Test\\modules', 'main')
C:\Users\YangQing\PycharmProjects\Test\modules\main
C:\Users\YangQing\PycharmProjects\Test\modules\main\test.log
C:\Users\YangQing\PycharmProjects\Test
C:\Users\YangQing\PycharmProjects
Test
从上述例子中可以看出,若路径字符串最后一个字符是\,则只有文件夹部分有值;若路径字符串中均无\,则只有文件名部分有值。若路径字符串有\,且不在最后,则文件夹和文件名均有值。且返回的文件夹的结果不包含\.
(5)查看文件的时间和大小
os.path.getmtime(path):文件或文件夹的最后修改时间,从新纪元到访问时的秒数
os.path.getatime(path):文件或文件夹的最后访问时间,从新纪元到访问时的秒数
os.path.getctime(path):文件或文件夹的创建时间,从新纪元到访问时的秒数
os.path.getsize(path):文件或文件夹的大小,若是文件夹返回0
print os.path.getatime('C:\Users\YangQing\PycharmProjects\Test')
print os.path.getctime('C:\Users\YangQing\PycharmProjects\Test')
print os.path.getmtime('C:\Users\YangQing\PycharmProjects\Test')
print os.path.getsize('C:\Users\YangQing\PycharmProjects\Test')
print os.path.getsize('C:\Users\YangQing\PycharmProjects')
1510209680.24
1508206623.84
1510209680.24
4096
0
(6)查看文件是否存在
os.path.exists(path):文件或文件夹是否存在,返回True 或 False。
print os.path.exists('..\class.py')
print os.path.exists('C:\Users\YangQing\PycharmProjects\Test')
False
True
os模块还有很多其他的方法,就不一一举例,如下:
os.remove()——删除指定文件
os.rmdir()——删除指定目录
os.mkdir()——创建目录
os.makedirs() ——递归创建目录
os.path.isfile()——判断指定对象是否为文件。是返回True,否则False
os.path.isdir()——判断指定对象是否为目录。是True,否则False
commands模块
通常我们调用os.system(cmd) 只能获得命令是否能执行成功。即结果为0或者非0标识是否执行成功。而有时我们希望即获取到是否成功,同时也获取命令的执行结果。这时就可以使用commands了,通过它可以同时获取命令的执行结果输出和结果。(要注意的是commands是Linux环境下独有的)
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import commands
>>> commands.getoutput('ls')
'anaconda-ks.cfg\nbootstrap.xml?mac=20:89:6f:f9:1d:63\nbrain\ninstall.log\ninstall.log.syslog\ntest.py'
>>> rs=commands.getoutput('ls')
>>> print rs,type(rs)
anaconda-ks.cfg
bootstrap.xml?mac=20:89:6f:f9:1d:63
brain
install.log
install.log.syslog
test.py <type 'str'>
>>> re,rs=commands.getstatusoutput('ip addr list')
>>> print re,rs
0 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 66:95:73:bf:5f:97 brd ff:ff:ff:ff:ff:ff
inet 218.207.221.92/27 brd 218.207.221.95 scope global eth0
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether b2:91:14:7e:78:ae brd ff:ff:ff:ff:ff:ff
>>> print re
0
>>> print rs
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 66:95:73:bf:5f:97 brd ff:ff:ff:ff:ff:ff
inet 218.207.221.92/27 brd 218.207.221.95 scope global eth0
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether b2:91:14:7e:78:ae brd ff:ff:ff:ff:ff:ff
>>>
commands.getstatusoutput(cmd):用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result),其中 status为int类型,result为string类型。cmd执行的方式是{ cmd ; } 2>&1, 这样返回结果里面就会包含标准输出和标准错误。
commands.getoutput(cmd) :只返回执行的结果, 忽略返回值。
sys模块
sys模块功能多,可以使用dir(sys)查看:
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_git', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
这里就学习下常用的方法,如下:
sys.argv: 实现从程序外部向程序传递参数。
sys.exit([arg]): 程序中间的退出,arg=0为正常退出。
sys.getdefaultencoding(): 获取系统当前编码,一般默认为ascii。
sys.getfilesystemencoding(): 获取文件系统使用编码方式,Windows下返回'mbcs',Mac和Linux下返回'utf-8'。
sys.path: 获取指定模块搜索路径的字符串集合,可以将写好的模块放在得到的某个路径下,就可以在程序中import时正确找到。
sys.platform: 获取当前系统平台。
sys.modules 是一个dictionary,表示系统中所有可用的module。
sys.stdin,sys.stdout,sys.stderr: stdin , stdout , 以及stderr 变量包含与标准I/O 流对应的流对象. 如果需要更好地控制输出,而print 不能满足你的要求, 它们就是你所需要的. 你也可以替换它们, 这时候你就可以重定向输出和输入到其它设备( device ), 或者以非标准的方式处理它们。
#Windows
import sys
# print dir(sys)
print sys.path
print sys.platform
print sys.modules
print sys.getdefaultencoding()
print sys.getfilesystemencoding()
['C:\\Users\\YangQing\\PycharmProjects\\Test\\modules\\main', 'C:\\Users\\YangQing\\PycharmProjects\\Test', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
win32
{'copy_reg': <module 'copy_reg' from 'C:\Python27\lib\copy_reg.pyc'>, 'sre_compile': <module 'sre_compile' from 'C:\Python27\lib\sre_compile.pyc'>, 'locale': <module 'locale' from 'C:\Python27\lib\locale.pyc'>, '_sre': <module '_sre' (built-in)>, 'functools': <module 'functools' from 'C:\Python27\lib\functools.pyc'>, 'encodings': <module 'encodings' from 'C:\Python27\lib\encodings\__init__.pyc'>, 'site': <module 'site' from 'C:\Python27\lib\site.pyc'>, '__builtin__': <module '__builtin__' (built-in)>, 'sysconfig': <module 'sysconfig' from 'C:\Python27\lib\sysconfig.pyc'>, 'operator': <module 'operator' (built-in)>, '__main__': <module '__main__' from 'C:/Users/YangQing/PycharmProjects/Test/modules/main/M_os.py'>, 'types': <module 'types' from 'C:\Python27\lib\types.pyc'>, 'encodings.encodings': None, 'encodings.gbk': <module 'encodings.gbk' from 'C:\Python27\lib\encodings\gbk.pyc'>, 'abc': <module 'abc' from 'C:\Python27\lib\abc.pyc'>, '_weakrefset': <module '_weakrefset' from 'C:\Python27\lib\_weakrefset.pyc'>, 'encodings._codecs_cn': None, 'errno': <module 'errno' (built-in)>, 'encodings.codecs': None, 'sre_constants': <module 'sre_constants' from 'C:\Python27\lib\sre_constants.pyc'>, 're': <module 're' from 'C:\Python27\lib\re.pyc'>, '_abcoll': <module '_abcoll' from 'C:\Python27\lib\_abcoll.pyc'>, 'ntpath': <module 'ntpath' from 'C:\Python27\lib\ntpath.pyc'>, '_codecs': <module '_codecs' (built-in)>, 'encodings._multibytecodec': None, 'nt': <module 'nt' (built-in)>, '_warnings': <module '_warnings' (built-in)>, 'genericpath': <module 'genericpath' from 'C:\Python27\lib\genericpath.pyc'>, 'stat': <module 'stat' from 'C:\Python27\lib\stat.pyc'>, 'zipimport': <module 'zipimport' (built-in)>, 'encodings.__builtin__': None, 'warnings': <module 'warnings' from 'C:\Python27\lib\warnings.pyc'>, 'UserDict': <module 'UserDict' from 'C:\Python27\lib\UserDict.pyc'>, '_multibytecodec': <module '_multibytecodec' (built-in)>, 'commands': <module 'commands' from 'C:\Python27\lib\commands.pyc'>, 'sys': <module 'sys' (built-in)>, 'codecs': <module 'codecs' from 'C:\Python27\lib\codecs.pyc'>, 'os.path': <module 'ntpath' from 'C:\Python27\lib\ntpath.pyc'>, '_functools': <module '_functools' (built-in)>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_locale': <module '_locale' (built-in)>, 'signal': <module 'signal' (built-in)>, 'traceback': <module 'traceback' from 'C:\Python27\lib\traceback.pyc'>, 'linecache': <module 'linecache' from 'C:\Python27\lib\linecache.pyc'>, 'encodings.aliases': <module 'encodings.aliases' from 'C:\Python27\lib\encodings\aliases.pyc'>, 'exceptions': <module 'exceptions' (built-in)>, 'sre_parse': <module 'sre_parse' from 'C:\Python27\lib\sre_parse.pyc'>, 'os': <module 'os' from 'C:\Python27\lib\os.pyc'>, '_weakref': <module '_weakref' (built-in)>}
ascii
mbcs
#Linux
>>> import sys
>>> sys.path
['', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages']
>>> sys.platform
'linux2'
>>> sys.modules
{'copy_reg': <module 'copy_reg' from '/usr/lib64/python2.6/copy_reg.pyc'>, 'encodings': <module 'encodings' from '/usr/lib64/python2.6/encodings/__init__.pyc'>, 'site': <module 'site' from '/usr/lib64/python2.6/site.pyc'>, '__builtin__': <module '__builtin__' (built-in)>, '__main__': <module '__main__' (built-in)>, 'encodings.encodings': None, 'abc': <module 'abc' from '/usr/lib64/python2.6/abc.pyc'>, 'posixpath': <module 'posixpath' from '/usr/lib64/python2.6/posixpath.pyc'>, 'errno': <module 'errno' (built-in)>, 'encodings.codecs': None, '_abcoll': <module '_abcoll' from '/usr/lib64/python2.6/_abcoll.pyc'>, 'types': <module 'types' from '/usr/lib64/python2.6/types.pyc'>, '_codecs': <module '_codecs' (built-in)>, '_warnings': <module '_warnings' (built-in)>, 'genericpath': <module 'genericpath' from '/usr/lib64/python2.6/genericpath.pyc'>, 'stat': <module 'stat' from '/usr/lib64/python2.6/stat.pyc'>, 'zipimport': <module 'zipimport' (built-in)>, 'encodings.__builtin__': None, 'warnings': <module 'warnings' from '/usr/lib64/python2.6/warnings.pyc'>, 'UserDict': <module 'UserDict' from '/usr/lib64/python2.6/UserDict.pyc'>, 'encodings.utf_8': <module 'encodings.utf_8' from '/usr/lib64/python2.6/encodings/utf_8.pyc'>, 'sys': <module 'sys' (built-in)>, 'codecs': <module 'codecs' from '/usr/lib64/python2.6/codecs.pyc'>, 'readline': <module 'readline' from '/usr/lib64/python2.6/lib-dynload/readline.so'>, 'os.path': <module 'posixpath' from '/usr/lib64/python2.6/posixpath.pyc'>, 'commands': <module 'commands' from '/usr/lib64/python2.6/commands.pyc'>, 'signal': <module 'signal' (built-in)>, 'linecache': <module 'linecache' from '/usr/lib64/python2.6/linecache.pyc'>, 'posix': <module 'posix' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from '/usr/lib64/python2.6/encodings/aliases.pyc'>, 'exceptions': <module 'exceptions' (built-in)>, 'abrt_exception_handler': <module 'abrt_exception_handler' from '/usr/lib64/python2.6/site-packages/abrt_exception_handler.pyc'>, 'os': <module 'os' from '/usr/lib64/python2.6/os.pyc'>}
>>> sys.getdefaultencoding()
'ascii'
>>> sys.getfilesystemencoding()
'UTF-8'
>>>
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341