shell中執行python檔案

2021-08-04 19:31:13 字數 1289 閱讀 8848

python中想在shell中呼叫乙個test.py檔案裡面的方法。

test.py檔案裡面的內容如下:

[python]view plain

copy

print?

deflistfea():  

print

"this is myself"

deflistfeat(fea):  

print

"this is:"

, fea  

deflistfear(fee, fea):  

print

"this is:"

, fee, 

"add"

, fea  

class

******test(object):  

"****** test"

defrunone():  

print

"runone"

defruntwo(

self

):  

print

"this has:"

,self

defrunthree(

self

,value):  

print

"this is:"

,value  

如何在shell中呼叫py中的內容呢。

其實主要加幾行就夠了:

[python]view plain

copy

print?

import

sys 

#引入sys庫體

) #往系統路徑中加入自己存放py檔案的位址

然後就可以開始通過import的方法匯入相關的方法和內容了

[python]view plain

copy

print?

from

test 

import

* #從test.py檔案中載入所有的內容。

順帶提一下python檔案進行編譯的方法:

[python]view plain

copy

print?

import

py_compile 

#匯入編譯庫

py_compile.compile("c://mypython"

) #對test.py檔案進行編譯

因為也是剛開始學習,所以可能一些名詞用的不是很準確,大家包涵了。

Python指令碼中執行shell命令

system 其中最後乙個0是這個命令的返回值,為0表示命令執行成功。使用system無法將執行的結果儲存起來。這裡寫描述popen 獲取命令執行的結果,但是沒有命令的執行狀態,這樣可以將獲取的結果儲存起來放到list中。commands 可以很方便的取得命令的輸出 包括標準和錯誤輸出 和執行狀態位...

python中shell執行知識點

更多程式設計教程請到 菜鳥教程 高州陽光論壇 人人影視 os.system system方法會建立子程序執行外部程式,方法只返回外部程式的執行結果。這個方法比較適用於外部程式沒有輸出結果的情況。import os os.system ls commands.getstatusoutput 使用com...

python執行shell命令

在此比較一下兩種方法執行系統命令的方法,以方便於日後運用 1.os.system system command exit status execute the command a string in a subshell.僅僅在乙個子終端執行系統命令,而不能獲取命令執行後的返回資訊.os.syste...