python os sys_python os模块sys模块常用方法
官方文檔看這里?https://docs.python.org/3.5/library/os.html
http://www.cnblogs.com/wupeiqi/articles/5501365.html
os.path.exists(file) 如果file存在于當(dāng)前目錄下,返回True,否則返回False
os.path.abspath(file) 返回file的絕對(duì)路徑
os.path.dirname(file) 返回file的上級(jí)目錄名
sys.path.append(path) 添加path到環(huán)境變量
os.system(command)
def system(*args, **kwargs): #real signature unknown
"""Execute the command in a subshell."""
pass
View Code
Execute the command (a string) in a subshell. 系統(tǒng)命令如果本身就會(huì)打印結(jié)果,那么你會(huì)在屏幕上看到結(jié)果。返回值是進(jìn)程的退出狀態(tài),若成功執(zhí)行,則返回值為0,若有報(bào)錯(cuò),返回值為錯(cuò)誤代碼。
os.popen(command[, mode[, bufsize]])
#Supply os.popen()
def popen(cmd, mode="r", buffering=-1):if notisinstance(cmd, str):raise TypeError("invalid cmd type (%s, expected string)" %type(cmd))if mode not in ("r", "w"):raise ValueError("invalid mode %r" %mode)if buffering == 0 or buffering isNone:raise ValueError("popen() does not support unbuffered streams")importsubprocess, ioif mode == "r":
proc=subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
bufsize=buffering)return_wrap_close(io.TextIOWrapper(proc.stdout), proc)else:
proc=subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
bufsize=buffering)return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
View Code
執(zhí)行系統(tǒng)命令,執(zhí)行結(jié)果寫到一個(gè)臨時(shí)文件里面,返回值是這個(gè)打開(kāi)的文件對(duì)象。mode默認(rèn)值為r,即默認(rèn)以只讀方式打開(kāi)文件;buffersize默認(rèn)是系統(tǒng)緩沖區(qū)大小(buffer緩沖,此概念適用于磁盤寫數(shù)據(jù);cache緩存,此概念適用于磁盤讀數(shù)據(jù))。
既然返回的是一個(gè)文件對(duì)象,那么接下來(lái)可以理解os.popen().read(),是把這個(gè)文件對(duì)象中的內(nèi)容讀出來(lái),返回值就是文件中的內(nèi)容。
os.path.exists('文件名')
判斷文件是否存在,存在返回True,不存在返回False
defmkdir(*args, **kwargs): # real signature unknown"""Create a directory.If dir_fd is not None, it should be a file descriptor open to a directory,and path should be relative; path will then be relative to that directory.dir_fd may not be implemented on your platform.If it is unavailable, using it will raise a NotImplementedError.The mode argument is ignored on Windows."""pass
總結(jié)
以上是生活随笔為你收集整理的python os sys_python os模块sys模块常用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 联想拯救者笔记本怎么进去bios 联想拯
- 下一篇: 联想小新510s怎么安装系统 如何在联想