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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/10/8 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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模块常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。