简体   繁体   中英

Running Linux commands using python script

I am working on a python script which execute basic shell commands. like -

  • check out a shell file from server - ct co -nc file_name
  • set an environment variable in shell terminal - setenv variable value
  • check in a file into server - ct ci -nc file_name.

Basically I want to know how to execute basic commands through python script. 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?

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

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

or directly pass the string as a command

cmd = 'ct co -nc file_name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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