我真的很喜歡 jupyter 像這樣內聯混合 bash 命令的能力:for d in list_of_dirs: ! ls cbo318w但是,有沒有辦法將這些內聯命令之一的輸出分配給 python 變量?例如作為python列表?
3 回答

UYOU
TA貢獻1878條經驗 獲得超4個贊
os.popen 為此工作。popen - 打開一個管道到或從命令。返回值是一個連接到管道的打開文件對象,可以讀取。split('\n') 將輸出轉換為列表
import os
list_of_ls = os.popen("ls").read().split('\n')
print list_of_ls

BIG陽
TA貢獻1859條經驗 獲得超6個贊
像這樣的東西?
代碼:
python_list = []
for d in list_of_dirs:
temp_list = !ls 1v8rtwc
python_list.extend(temp_list)
添加回答
舉報
0/150
提交
取消