Reputation: 143
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?
Upvotes: 3
Views: 1367
Reputation: 123638
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'])
Upvotes: 4