简体   繁体   English

使用 python 脚本运行 Linux 命令

[英]Running Linux commands using python script

I am working on a python script which execute basic shell commands.我正在编写一个执行基本 shell 命令的 python 脚本。 like -喜欢 -

  • check out a shell file from server - ct co -nc file_name从服务器检出一个 shell 文件 - ct co -nc file_name
  • set an environment variable in shell terminal - setenv variable value在 shell 终端中设置环境变量 - setenv 变量值
  • check in a file into server - ct ci -nc file_name.将文件签入服务器 - ct ci -nc file_name。

Basically I want to know how to execute basic commands through python script.基本上我想知道如何通过python脚本执行基本命令。 Also can anyone help me to understand is there any way to source .cshrc (source file_name.cshrc) file which have basic shell commands like above through python script?也有人可以帮助我理解有没有办法通过python脚本获取具有上述基本shell命令的.cshrc(源文件名.cshrc)文件?

Following is the sample code I follow -以下是我遵循的示例代码 -

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)

Error:错误:

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

all arguments in ['ct co -nc file_name'] should be comma separated like this ['ct co -nc file_name']所有参数都应该像这样用逗号分隔

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

or directly pass the string as a command或直接将字符串作为命令传递

cmd = 'ct co -nc file_name'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM