首页 文章

如何在Mac上删除python脚本中的bash历史记录?

提问于
浏览
3

我想在我的Macbook Pro上用python脚本删除bash历史记录 .

我知道用bash shell删除bash历史记录的两种方法

1.rm~ / .bash_history

2.history -c

但是这些命令在带有子进程的python脚本中不起作用:

1.rm~ / .bash_history

import subprocess
subprocess.call([‘rm’, ‘~/.bash_history'])

错误:

rm:〜/ .bash_history:没有这样的文件或目录

2.history -c

import subprocess
subprocess.call(['history', '-c'])

错误:

文件“test.py”,第8行,在subprocess.call(['history',' - c'])文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ subprocess.py“,line> 524,在调用返回Popen(* popenargs,** kwargs).wait()文件”/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess . py“,line> 711,在init errread,errwrite)文件”/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py“,行> 1308,在_execute_child中引发child_exception

有任何想法吗?

1 回答

  • 4

    你有两个问题:

    首先,python不理解 ~ ,你需要扩展它:

    subprocess.call(['rm', os.path.expanduser('~/.bash_history')])
    

    其次, history 是一个内置的shell . 使用shell来调用它:

    subprocess.call(['bash', '-c', 'history -c'])
    

相关问题