如何在Mac上的python脚本中删除bash历史记录?
作者:互联网
我想在Macbook Pro上使用python脚本删除bash历史记录.
我知道两种使用bash shell删除bash历史记录的方法
1.rm〜/ .bash_history
2.历史-c
但是这些命令在带有子进程的python脚本中不起作用:
1.rm〜/ .bash_history
import subprocess
subprocess.call([‘rm’, ‘~/.bash_history'])
错误:
rm:〜/ .bash_history:没有这样的文件或目录
2.历史-c
import subprocess
subprocess.call(['history', '-c'])
错误:
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
有任何想法吗?
解决方法:
您在这里有两个问题:
首先,python无法理解〜,您需要对其进行扩展:
subprocess.call(['rm', os.path.expanduser('~/.bash_history')])
第二,历史是内置的shell.使用外壳程序来调用它:
subprocess.call(['bash', '-c', 'history -c'])
标签:bash,delete-file,history,python,macos 来源: https://codeday.me/bug/20191030/1964351.html