描述
方法popen()從命令打開管道。返回值是連接到管道的打開文件對象,可以根據模式是 'r'(默認)還是 'w' 讀取或寫入。 bufsize 參數與 open() 函數中的含義相同.
用法
以下是語法popen()方法 -
os.popen(command[, mode[, bufsize]])
參數
command- 這是使用的命令。
mode− 此模式可為 'r'(默認)或 'w'。
bufsize− 如果緩衝值設置為 0,則不進行緩衝。如果緩衝值為 1,則在訪問文件時將執行行緩衝。如果您將緩衝值指定為大於 1 的整數,則將使用指定的緩衝區大小執行緩衝操作。如果為負,則緩衝區大小為係統默認值(默認行為)。
返回值
此方法返回連接到管道的打開文件對象。
示例
下麵的例子展示了 popen() 方法的用法。
# !/usr/bin/python3
import os, sys
# using command mkdir
a = 'mkdir nwdir'
b = os.popen(a,'r',1)
print (b)
結果
當我們運行上述程序時,它會產生以下結果 -
<os._wrap_close object at 0x7fb599240b70>
相關用法
- Python 3 os.pathconf()用法及代碼示例
- Python 3 os.pipe()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 os.rmdir()用法及代碼示例
- Python 3 os.fdopen()用法及代碼示例
- Python 3 os.fdatasync()用法及代碼示例
- Python 3 os.isatty()用法及代碼示例
- Python 3 os.rename()用法及代碼示例
- Python 3 os.walk()用法及代碼示例
- Python 3 os.renames()用法及代碼示例
- Python 3 os.makedirs()用法及代碼示例
- Python 3 os.utime()用法及代碼示例
- Python 3 os.tcgetpgrp()用法及代碼示例
- Python 3 os.statvfs()用法及代碼示例
- Python 3 os.lchown()用法及代碼示例
- Python 3 os.mknod()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.popen() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。