Append to File Using Subprocess in Python
Question: How do I append to a file without opening it using Linux echo command? I've
tried all sort of possibilities but unable to achieve the result. I'm not sure
what I'm missing.

Documentation said to type out the exact command when shell is set to 'True'
_(Yes with security risk)_. The echo command works when I type it in Linux
terminal but not through subprocess. I don't see the "test.txt" with 'This is
a test string.'

    
    
    >>> string = 'This is a test string.'    
    >>> cmd = 'echo \"' + string + '\" >> test.txt'
    >>> cmd
    'echo "This is a test string." >> test.txt'
    >>> 
    >>> subprocess.check_call (cmd, shell = True)
    0
    >>> 
    >>> subprocess.call (['echo', '\"', string, '\"', ' >> ', ' test.txt'])
    0
    >>> 
    


Answer: As discussed in [this answer](http://stackoverflow.com/a/26417712/1142167),
when using, `shell=True`, you should pass a string, not a list, as the first
argument. Thus the correct code would be:

    
    
    subprocess.call('echo "foo" >> bar.txt', shell=True)
    

Demonstration:

    
    
    >>> import subprocess
    >>> subprocess.call('echo "foo" >> /tmp/bar.txt', shell=True)
    0
    >>> open('/tmp/bar.txt').read()
    'foo\n'
    >>>
    

