从 python3 运行 bash 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37865608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:47:12 来源:igfitidea点击:
running bash command from python3
提问by BaRud
I am trying to remove some file (from my linux machine), except few:
我正在尝试删除一些文件(从我的 linux 机器中),除了少数几个:
touch INCAR KPOINTS foo bar
$ls
bar foo INCAR KPOINTS
$python3 mini.py
Job Done
$ls
bar foo INCAR KPOINTS
The mini.py
is:
该mini.py
是:
#!/usr/bin/python3
import subprocess
subprocess.run(['rm', '-f', '!(INCAR|KPOINTS|PO*|*.sh)'])
print("Job Done")
As can be seen in the output of mini.py
, its notgiving any error but neither its doing its job.
从 的输出中可以看出mini.py
,它没有给出任何错误,但也没有完成它的工作。
What I am doing wrong here?
我在这里做错了什么?
回答by Fabricator
回答by Hans
you can also use
你也可以使用
import os
os.system('rm -f !(INCAR|KPOINTS|PO*|*.sh)')