日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python os sys_python os模块sys模块常用方法

發(fā)布時(shí)間:2024/10/8 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python os sys_python os模块sys模块常用方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

官方文檔看這里?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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。