美文网首页
Python3 OS module

Python3 OS module

作者: 止杀恶人 | 来源:发表于2018-10-02 18:02 被阅读0次

官方文档:https://docs.python.org/3/library/os.html

os.ctermid()
>>> os.ctermid()
'/dev/tty'
os.environ
>>> os.environ
environ({'HOSTNAME': 'lishf-dev.cloud.mos', 'SHELL': '/bin/bash', 'TERM': 'xterm-256color', 'HISTSIZE': '50000', 'QTDIR': '/usr/lib64/qt-3.3', 'QT_GRAPHICSSYSTEM_CHECKED': '1', 'MOS_URL': 'https://mosapi.meituan.com/mcs/v1', 'USER': 'root', 'LS_COLORS': 'rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:', 'SUDO_USER': 'lishf', 'SUDO_UID': '1000', 'USERNAME': 'root', 'PATH': '/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/root/bin', 'MAIL': '/var/spool/mail/root', 'PWD': '/root', 'AWS_SECRET_ACCESS_KEY': 'VYDGDri5LjK2cqkoDQx9r4kMiETx4Af6gK+PYbXB', 'LANG': 'en_US.utf8', 'HISTCONTROL': 'ignoredups', 'AWS_ACCESS_KEY_ID': 'AKIAIFN3V4X74U2VCUAA', 'SHLVL': '1', 'SUDO_COMMAND': '/bin/bash', 'HOME': '/root', 'MOS_SECRET': 'd37331d1a1a34a97a725cc4b56212e8f', 'LOGNAME': 'root', 'MOS_ACCESS': 'cf641b7318c544318c8f377591cf4ddd', 'LC_CTYPE': 'zh_CN.UTF-8', 'LESSOPEN': '||/usr/bin/lesspipe.sh %s', 'SUDO_GID': '1000', 'HISTTIMEFORMAT': 'root %F %T ', '_': '/bin/python3'})

putenv() 增加环境变量
unsetenv() 移除环境变量

os.environb

Bytes version of environ: a mapping object representing the environment as byte strings. environ and environb are synchronized (modify environb updates environ, and vice versa).

environb is only available if supports_bytes_environ is True.

New in version 3.2.

os.chdir(path)

变更当前工作目录

os.fchdir(fd)

方法通过文件描述符改变当前工作目录。
fd -- 文件描述符
参考:http://www.runoob.com/python/os-fchdir.html

os.getcwd()

获取当前工作目录

>>> os.getcwd()
'/root'
os.getenv(key, default=None)
>>> os.getenv("PATH")
'/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/root/bin'
os.getenvb(key, default=None)

Return the value of the environment variable key if it exists, or default if it doesn’t. key, default and the result are bytes.

os.get_exec_path(env=None)
>>> os.get_exec_path(env=None)
['/usr/local/sbin', '/sbin', '/bin', '/usr/sbin', '/usr/bin', '/usr/local/bin', '/root/bin']

New in version 3.2.

os.getegid

Return the effective group id of the current process. This corresponds to the “set id” bit on the file being executed in the current process.

Availability: Unix.

os.geteuid()

Return the current process’s effective user id.

Availability: Unix.

os.getgid()

Return the real group id of the current process.

Availability: Unix.

os.getuid()

Return the real group id of the current process.

Availability: Unix.

os.getgrouplist(user, group)

Return list of group ids that user belongs to. If group is not in the list, it is included; typically, group is specified as the group ID field from the password record for user.

Availability: Unix.

New in version 3.3.

>>> os.getgrouplist('root',0)
[0]
>>> os.getgrouplist('lishf',1000)
[1000]
os.getgroups()
>>> os.getgroups()
[0]
os.getlogin()

此处os.getlogin()只是获取到最开始登陆的用户lishf,而之后sudo到root检测不到,实际使用时要注意;另外两种方式没这个问题

>>> os.getlogin()
'lishf'

>>> import getpass
>>> getpass.getuser()
'root'

>>> import pwd
>>> pwd.getpwuid(os.getuid())[0]
'root'

Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use getpass.getuser() since the latter checks the environment variables LOGNAME or USERNAME to find out who the user is, and falls back to pwd.getpwuid(os.getuid())[0] to get the login name of the current real user id.

Availability: Unix, Windows.

相关文章

网友评论

      本文标题:Python3 OS module

      本文链接:https://www.haomeiwen.com/subject/zalaoftx.html