我想在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'])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。