1

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

skameswa
  • 33
  • 1
  • 4

1 Answers1

0

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')
3Doubloons
  • 2,088
  • 14
  • 26
  • One could do that just as easily by using the stdin argument to `communicate()` to pass a string. – Charles Duffy Jan 13 '16 at 22:27
  • @3Doubloons: I dint completely understand your suggestion. Can you pls provide more information with some example snippet if possible. Thanks. – skameswa Jan 13 '16 at 23:12
  • @3Doubloons: Thanks for the snippet. I tried ur method as well as Charles Duffy's said and i get this error message: Traceback (most recent call last): File "testshell.py", line 16, in p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 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 – skameswa Jan 14 '16 at 19:51
  • subprocess.call expects a list of strings. You can use the shlex module to split a string into its equivalent arguments, but in this case, you should just build a list of the arguments you want to pass – 3Doubloons Jan 14 '16 at 20:11
  • @3Doubloons: Never mind, It worked. Thanks a lot for your help. Appreciate it !! – skameswa Jan 14 '16 at 20:17