subprocess.call command for executing "ci -u <filename>" - python

I am trying to execute a python script that first creates a new file(if it does not exist) and then executes ci(on the newly created file) to create a rcs file with initial revision number. But when I run the script it asks me for description and ending with period '.'. I want this part to be automated with a default description and create the rcs file without user input. Any help would be much appreciated. Following is my code:
import os
import subprocesss
if os.path.isfile(location):
print "File already exists"
else:
f = open(location,'a')
subprocess.call(["ci", "-u", location])
f.close()
print "new file has been created"
I tried this and I am getting the following error:
import os
import subprocess
if os.path.isfile(location):
print "File already exists"
else:
f = open(location,'a')
cmd = "ci -u "+location
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
stdout_data = p.communicate(input='change\n.')[0]
f.close()
print "new file has been created"
Traceback (most recent call last):
File "testshell.py", line 15, in
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
File "/usr/local/pythonbrew/pythons/Python-2.7/lib/python2.7/subprocess.py", line 672, in init
errread, errwrite)
File "/usr/local/pythonbrew/pythons/Python-2.7/lib/python2.7/subprocess.py", line 1201, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

You can use subprocess.call's stdin argument to give the subprocess a file-like object to use as its standard input (what you would enter by hand).
The StringIO module contains a class called StringIO that offers a file-like interface to an in-memory string.
Combining these two pieces together will let you send a specific string to ci, as if the user had entered it manually
from StringIO import StringIO
import subprocess
...
subprocess.call(['command', 'with', 'args'], stdin=StringIO('StandardInput'))
Alternately, as CharlesDuffy suggests, you can use Popen and its communicate method:
import subprocess
proc = subprocess.Popen(['command', 'with', 'args'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate('StandardInput')

Related

subprocess gives an error. "The system cannot find the file specified"

This is my code:
import urllib
import requests
from bs4 import *
from subprocess import Popen,PIPE
import os
connectString = 'SYSTEM/mediadot123'
def runSqlQuery(sqlCommand, connectString):
session = Popen(['sqlplus', '-S', connectString], stdin=PIPE, stdout=PIPE, stderr=PIPE)
session.stdin.write(sqlCommand)
return session.communicate()
session = Popen(['sqlplus','-S','hr/hr'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = session.communicate()
sqlCommand = "insert into food(title, recipe, image) values ('bla','bla','bla');"
queryResult, errorMessage = runSqlQuery(sqlCommand, connectString)
print queryResult
And it gives me the following error:
C:\Python27\python.exe C:/Users/Umer/PycharmProjects/DATACRAWLER/main.py
Traceback (most recent call last):
File "C:/Users/Umer/PycharmProjects/DATACRAWLER/main.py", line 38, in <module>
session = subprocess.Popen(['sqlplus','-S','hr/hr'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Consider using an absolute path for your command-execution.
Some binaries are not located in PATH depending on your user, system and software installation.
To find out where sqlplus resides, run the following in cmd.exe: where sqlplus and that should give you the absolute path.
Then simply do:
Popen(['C:/path/sqlplus.exe', '-S', ...])
Also to find out what's actually in your PATH environment variable, you could do the following:
print(os.environ['PATH'])

Reading and writing from and to different files in Python

I am trying to read the commands from "command.txt" file and want to redirect the output of this commands to "output.txt", contents of command.txt
ps -a
free
So far I came up with this code which for certain reason is not good and fails to execute.
import os
import sys
import subprocess
with open('output.txt', 'w') as out_file, open('command.txt', 'r') as in_file:
for line in in_file:
output = subprocess.Popen(line, stdout=subprocess.PIPE)
print output
out_file.write(output)
I am getting the below error:
Error:
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7/Users/PythonTutorials/subprocess1.py
Traceback (most recent call last):
File "/Users/shandeepkm/PythonTutorials/subprocess1.py", line 9, in <module>
output = subprocess.Popen(line, stdout=subprocess.PIPE)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Process finished with exit code 1
Could anyone please suggest appropriate python code for this task.
I see two errors.
First, you have "command.txt" as the first line in your file. That definitely won't execute as a subprocess.
Also, your line
out_file.write(output)
needs to be tabbed under the for loop.
Now for the updated question:
The Popen constructor needs to take an array for the args. So instead of
'ps -a'
you need to pass
['ps', '-a']
Also, what gets returned from Popen isn't text. So altogether you need:
args = shlex.split(line)
output = subprocess.Popen(args, stdout=subprocess.PIPE).stdout.read()
Solution 1: Execute line by line
You can redirect to a file using the stdout parameter of Popen:
import subprocess
import shlex
with open('output.txt', 'wb') as outfile, open('command.txt') as infile:
for line in infile:
command = shlex.split(line)
if not command:
continue # Skip blank lines
try:
process = subprocess.Popen(command, stdout=outfile)
process.wait()
except OSError:
outfile.write('COMMAND ERROR: {}'.format(line))
In the code above, you redirect the output by pointing stdout to the output file's handle, no printing is needed. The code also guard against bad commands
Solution 2: Call the shell
If you are running under Linux or Mac, the following solution is simpler: by calling bash to execute the whole command.txt file and record the stdout and stderr. This should work under windows with cmd -c, but I don't have a Windows machine to try.
import subprocess
with open('output.txt', 'wb') as outfile:
command = ['bash', 'command.txt']
process = subprocess.Popen(command, stdout=outfile, stderr=outfile)
process.wait()

error while running Popen

I am running into following error when trying to run a command using Popen,what is wrong here?
cmd = "export COMMANDER_SERVER=commander.company.com"
Pipe = Popen(cmd, stdout=PIPE, stderr=PIPE)
(output, error) = Pipe.communicate()
Error:-
Traceback (most recent call last):
File "test_ectool.py", line 26, in <module>
main()
File "test_ectool.py", line 13, in main
Pipe = Popen(cmd, stdout=PIPE, stderr=PIPE)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
You need to separate the arguments from the command and give a list to Popen.
As Kenster's comment said, even if your command worked, you would only succeed in modifying an environmental variable inside a sub-shell not the main shell.
You will not be able run run export this way, because it is not a program. It is a bash built-in.
Here is an example of a command that does work, with the correct semantics.
from subprocess import Popen,PIPE
cmd = "echo COMMANDER_SERVER=commander.company.com"
Pipe = Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
(output, error) = Pipe.communicate()
print output
merlin2011's answer is incorrect regarding the command string for Popen (point #1). From the Python docs:
args should be a sequence of program arguments or else a single string.
As other people have stated, the environment variable will not be saved. For that, you need to use os.environ['VARNAME'] = value. If you want to use other bash builtins, then you must pass shell=True as an argument to Popen.

How to use Subprocess in Windows

I am trying to save the result or function runcmd in the variable Result.
Here is what I have tried:
import subprocess
def runcmd(cmd):
x = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Result = x.communicate(stdout)
return Result
runcmd("dir")
When I run ths code, I get this result:
Traceback (most recent call last):
File "C:\Python27\MyPython\MyCode.py", line 7, in <module>
runcmd("dir")
File "C:\Python27\MyPython\MyCode.py", line 4, in runcmd
x = subprocess.Popen(cmd, stdout=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
What could I do to fix this?
I think what you are looking for is os.listdir()
check out the os module for more info
an example:
>>> import os
>>> l = os.listdir()
>>> print (l)
['DLLs', 'Doc', 'google-python-exercises', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.e
xe', 'README.txt', 'tcl', 'Tools', 'VS2010Cmd.lnk']
>>>
You could also read the output into a list:
result = []
process = subprocess.Popen('dir',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE )
for line in process.stdout:
result.append(line)
errcode = process.returncode
for line in result:
print(line)
As far as I know, dir is a built in command of the shell in Windows and thus not a file available for execution as a program. Which is probably why subprocess.Popen cannot find it. But you can try adding shell=True to the Popen() construtor call like this:
def runcmd(cmd):
x = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
return x.communicate(stdout)
runcmd("dir")
If shell=True doesn't help, you're out of luck executing dir directly. But then you can make a .bat file and put a call to dir there instead, and then invoke that .bat file from Python instead.
btw also check out the PEP8!
P.S As Mark Ransom pointed out in a comment, you could just use ['cmd', '/c', 'dir'] as the value of cmd instead of the .bat hack if shell=True fails to fix the issue.

checking the return code using python (MAC)

I have written a script that checks if an SVN Repo is up and running, the result is based on the return value.
import subprocess
url = " validurl"
def check_svn_status():
subprocess.call(['svn info'+url],shell=True)
def get_status():
subprocess.call('echo $?',shell=True)
def main():
check_svn_status()
get_status()
if __name__ == '__main__':
main()
The problem I'm facing is that if I change the url to something that does't exist I still get the return value as 0, but if I were to run this outside the script, i.e go to the terminal type svn info wrong url and then do a echo $? I get a return value of 1. But I can't re-create this in the python. Any guidelines ?
TraceBack after updating
Traceback (most recent call last):
File "svn_status.py", line 21, in <module>
main()
File "svn_status.py", line 15, in main
check_svn_status()
File "svn_status.py", line 8, in check_svn_status
p = sp.Popen(['svn info'], stdout=sp.PIPE, stderr=sp.PIPE)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or director
y
Why your approach does not work:
You invoke two independent subshells. The second shell does not know of the first shell and therefore does not have any information about the returncode of the process that was executed in the first shell.
Solution:
Use the subprocess module, spawn your subprocess directly (not through a subshell) and retrieve the returncode. Help yourself by reading the documentation of the module: http://docs.python.org/library/subprocess.html
There are several ways to achieve your goal. One simple way could be:
import subprocess as sp
p = sp.Popen(['command', 'arg1', 'arg2'], stdout=sp.PIPE, stderr=sp.PIPE)
stdout, stderr = p.communicate()
returncode = p.returncode
This way, you don't go through a subshell (shell=False by default), which is the recommended approach for various reasons. You directly catch the returncode of the spawned subprocess and you have full access to the subprocess' standard output and standard error.
subprocess.call returns the retcode, just store the result of your subprocess.call(['svn info'+url],shell=True)
http://docs.python.org/library/subprocess.html

Categories