簡體   English   中英

使用 python 腳本運行 Linux 命令

[英]Running Linux commands using python script

我正在編寫一個執行基本 shell 命令的 python 腳本。 喜歡 -

  • 從服務器檢出一個 shell 文件 - ct co -nc file_name
  • 在 shell 終端中設置環境變量 - setenv 變量值
  • 將文件簽入服務器 - ct ci -nc file_name。

基本上我想知道如何通過python腳本執行基本命令。 也有人可以幫助我理解有沒有辦法通過python腳本獲取具有上述基本shell命令的.cshrc(源文件名.cshrc)文件?

以下是我遵循的示例代碼 -

import sys
import subprocess
file_name = sys.argv[0]
print ("file name is ==>", file_name)
cmd = ['ct co -nc file_name']
time = subprocess.Popen (cmd, shell=True)
output,err = time.communicate()
print(output)

錯誤:

('given file name is ==>', 'script_test_sys.py')
/bin/sh: ct: command not found
None

['ct co -nc file_name']所有參數都應該像這樣用逗號分隔

cmd = ['ct', 'co', '-nc', 'file_name'] 

或直接將字符串作為命令傳遞

cmd = 'ct co -nc file_name'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM