OS module


In [2]:
import os
print os.name
print os.uname()


posix
('Darwin', 'gaofeng.local', '15.6.0', 'Darwin Kernel Version 15.6.0: Thu Sep  1 15:01:16 PDT 2016; root:xnu-3248.60.11~2/RELEASE_X86_64', 'x86_64')

1 Process Parameter

1.1 environ


In [4]:
for k, v in os.environ.items():
    print k,':',v


LESS : -R
LC_CTYPE : UTF-8
TERM_PROGRAM_VERSION : 361.1
LOGNAME : gaufung
USER : gaufung
PATH : /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Library/TeX/texbin
HOME : /Users/gaufung
ZSH : /Users/gaufung/.oh-my-zsh
TERM_PROGRAM : Apple_Terminal
TERM : xterm-color
Apple_PubSub_Socket_Render : /private/tmp/com.apple.launchd.TRa4EnwkzR/Render
SHLVL : 2
SECURITYSESSIONID : 186a7
XPC_FLAGS : 0x0
_ : /Users/gaufung/Anaconda/anaconda/bin/jupyter-notebook
TERM_SESSION_ID : 40F8BF53-E44D-4E5C-B952-186849629CAA
XPC_SERVICE_NAME : 0
JPY_PARENT_PID : 76767
SSH_AUTH_SOCK : /private/tmp/com.apple.launchd.Uguwt4L5K5/Listeners
SHELL : /bin/zsh
GIT_PAGER : cat
TMPDIR : /var/folders/k9/2cxh1k2115s_lw4wtq9mzj9m0000gp/T/
LSCOLORS : Gxfxcxdxbxegedabagacad
MPLBACKEND : module://ipykernel.pylab.backend_inline
CLICOLOR : 1
__CF_USER_TEXT_ENCODING : 0x1F6:0x0:0x0
PWD : /Users/gaufung
PAGER : cat

1.2 getcwd()

get current work directionary


In [6]:
os.getcwd()


Out[6]:
'/Users/gaufung/WorkSpace/Python/Data_Analytics_Learning_Note/python-statatics-tutorial/basic-theme/python-language'

1.3 process infomation

  • os.ctermid()
    Return the filename corresponding to the controlling terminal of the process.

Availability: Unix.

  • 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.getgroups()
    Return list of supplemental group ids associated with the current process.

Availability: Unix.

  • os.getlogin()
    Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is, or pwd.getpwuid(os.getuid())[0] to get the login name of the process’s real user id.

Availability: Unix.

  • os.getpgid(pid)
    Return the process group id of the process with process id pid. If pid is 0, the process group id of the current process is returned.

Availability: Unix.

New in version 2.3.

  • os.getpgrp()
    Return the id of the current process group.

Availability: Unix.

  • os.getpid()
    Return the current process id.

Availability: Unix, Windows.

  • os.getppid()
    Return the parent’s process id.

Availability: Unix.

2 File and Directionary operations

  • os.makedirs()
    可生成多层递归目录
  • os.removedirs()
    可删除多层递归空目录,若目录不为空则无法删除
  • os.mkdir()
    生成单级目录
  • os.rmdir()
    删除单级空目录,若目录不为空则无法删除,报错
  • os.pardir()
    获取当前目录的父目录字符串名
  • os.listdir()
    列出指定目录下的所有文件和子目录,包括隐藏文件
  • os.tmpfile()
    创建并打开‘w+b’一个新的临时文件
  • os.remove()
    删除一个文件
  • os.rename("oldname","newname")
    重命名文件
  • os.sep
    输出操作系统特定的路径分隔符。win下为"\",macx下为"/"
  • os.linesep
    输出当前平台使用的行终止符
  • os.pathsep
    输出用于分割文件路径的字符串
  • os.name
    输出字符串指示当前使用平台。win->'nt'; mac->'posix'
  • os.system(command)
    运行shell命令,其中command为 shell 命令

3 os.path Module

3.1 os.path.split()


In [7]:
os.path.split('/User/gaufung/1.py')


Out[7]:
('/User/gaufung', '1.py')

3.2 isFile or isdir


In [10]:
os.path.isdir(os.getcwd())


Out[10]:
True

In [13]:
os.path.isfile('/User/1.txt')


Out[13]:
False

3.3 exists


In [14]:
os.path.exists(os.getcwd())


Out[14]:
True

In [15]:
os.path.exists('/User/gaofeng')


Out[15]:
False

3.4 abspath


In [21]:
print os.curdir
os.path.abspath(os.curdir)


.
Out[21]:
'/Users/gaufung/WorkSpace/Python/Data_Analytics_Learning_Note/python-statatics-tutorial/basic-theme/python-language'

3.5 getsize


In [22]:
os.path.getsize(os.getcwd())


Out[22]:
544

3.6 splitext


In [24]:
os.path.splitext('1.txt')


Out[24]:
('1', '.txt')

3.7 join


In [28]:
os.path.join('/Users','gaofeng','1.txt')


Out[28]:
'/Users/gaofeng/1.txt'

3.8 basename and dirname


In [30]:
path = '/Users/gaufung/1.txt'
print os.path.basename(path)
print os.path.dirname(path)


1.txt
/Users/gaufung