Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete bash history in python script on Mac?

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?

like image 280
Matt Avatar asked Feb 19 '26 02:02

Matt


1 Answers

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'])
like image 184
devnull Avatar answered Feb 21 '26 16:02

devnull



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!