我想在Macbook Pro上使用python脚本删除bash历史记录.
我知道两种使用bash shell删除bash历史记录的方法
1.rm?/ .bash_history
2.历史-c
但是这些命令在带有子进程的python脚本中不起作用:
1.rm?/ .bash_history
<code>import subprocess subprocess.call([‘rm’, ‘~/.bash_history']) </code>
错误:
rm:?/ .bash_history:没有这样的文件或目录
2.历史-c
<code>import subprocess subprocess.call(['history', '-c']) </code>
错误:
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无法理解?,您需要对其进行扩展:
<code>subprocess.call(['rm', os.path.expanduser('~/.bash_history')])
</code>
第二,历史是内置的shell.使用外壳程序来调用它:
<code>subprocess.call(['bash', '-c', 'history -c']) </code>
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!