Question

I want to delete bash history with a python script on my Macbook Pro.

I know two ways to delete bash history with bash shell

1.rm ~/.bash_history

2.history -c

But these command does not work in python script with subprocess:

1.rm ~/.bash_history

import subprocess
subprocess.call([‘rm’, ‘~/.bash_history'])

error:

rm: ~/.bash_history: No such file or directory

2.history -c

import subprocess
subprocess.call(['history', '-c'])

error:

File "test.py", line 8, in subprocess.call(['history', '-c']) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >524, in call return Popen(*popenargs, **kwargs).wait() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >711, in init errread, errwrite) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >1308, in _execute_child raise child_exception

Any ideas?

Was it helpful?

Solution

You have two questions here:

First, python doesn't understand ~, you need to expand it:

subprocess.call(['rm', os.path.expanduser('~/.bash_history')])

Second, history is a shell built-in. Use the shell to invoke it:

subprocess.call(['bash', '-c', 'history -c'])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top