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存在于當前目錄下,返回True,否則返回False
os.path.abspath(file) 返回file的絕對路徑
os.path.dirname(file) 返回file的上級目錄名
sys.path.append(path) 添加path到環境變量
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. 系統命令如果本身就會打印結果,那么你會在屏幕上看到結果。返回值是進程的退出狀態,若成功執行,則返回值為0,若有報錯,返回值為錯誤代碼。
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
執行系統命令,執行結果寫到一個臨時文件里面,返回值是這個打開的文件對象。mode默認值為r,即默認以只讀方式打開文件;buffersize默認是系統緩沖區大小(buffer緩沖,此概念適用于磁盤寫數據;cache緩存,此概念適用于磁盤讀數據)。
既然返回的是一個文件對象,那么接下來可以理解os.popen().read(),是把這個文件對象中的內容讀出來,返回值就是文件中的內容。
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
總結
以上是生活随笔為你收集整理的python os sys_python os模块sys模块常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想拯救者笔记本怎么进去bios 联想拯
- 下一篇: 联想小新510s怎么安装系统 如何在联想