Python Module OS commands

Source: Internet
Author: User
Tags posix

In automated operations and testing, it is often necessary to find operational files, such as locating a configuration file (thus reading the profile information), finding test reports (thus sending test report messages), frequently working with a large number of files and a large number of paths, which is dependent on the OS module for Python. Here are some common ways to learn the OS module.

>>> Import OS #导入os模块
>>> Help (OS) #查看os模块帮助文档, with detailed module-related functions and how to use them

>>> 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__ ', ' __built ' Ins__ ', ' __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 ', ' err No ', ' 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 Gets the platform that is currently in use, Windows returns ' NT '; Linux returns ' POSIX '

  #Linux 
Python 2.6.6 (r266:84292, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2< Br>type "Help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.name
' POSIX '
>>>
#Windows
Python 2.7.14 (v2.7.14 : 84471935ed, Sep, 20:25:58) [MSC v.1500-bit (AMD64)] on Win32
Type "Help", "copyright", "credits" or "Licens E "For more information.
>>> import os
>>> os.name
' nt '
>>>

(2) Os.system (command) Execute shell command

#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 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 qdisc NoOp State down Qlen 1000
Link/ether B2:91:14:7e:78:ae BRD FF:FF:FF:FF:FF:FF
0

(3) file under current path and path

OS.GETCWD (): View the current path.

Os.listdir (PATH): Lists all files under the directory. The type of the list is returned.

Os.path.abspath (PATH): Returns the absolute path.

Import OS
Print OS.GETCWD ()
Print Os.listdir (OS.GETCWD ())
Print Os.path.abspath ('. ')
Print Os.path.abspath ('.. ')

Operation Result:
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) View the folder part and file name section of the path

Os.path.split (path): Breaks the path into (folder, file name) and returns the tuple type.

Os.path.join (Path1,path2,...): Combines path and, if there is an absolute path, the previous path is deleted.

 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 ')

Run Result:
(', '. ')
(' 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

As can be seen from the above example, if the last character of the path string is \, only the folder part has a value, and if none in the path string, then only the file name part has a value. If the path string has \ and is not at the end, both the folder and the file name have values. And the result of the returned folder does not contain \.

(5)


(6)


This article is from the "Dreamscape" blog, make sure to keep this source http://dyqd2011.blog.51cto.com/3201444/1980528

Python Module OS commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.