![](/img/trans.png)
[英]Using soffice within python, Command works in terminal but not in Python subprocess
[英]using python .subprocess for terminal command
大家好,我想删除特定文件夹上的所有文件/文件夹,并做到这一点,我编写了以下代码:我有一个错误
def remove_file():
remove="sudo rm -rf !(packages_with_diff_branches.txt|packages_with_same_branches.txt)"
p = subprocess.Popen("""
%s
%s""" % (co_directory,remove),shell=True , executable='/bin/bash')
p.wait()
/bin/bash: -c: line 3: syntax error near unexpected token `('
/bin/bash: -c: line 3: ` sudo rm -rf !(packages_with_diff_branches.txt|packages_with_same_branches.txt)'
有没有人可以帮助我? 非常感谢
编辑 ** co_directory是全局变量**
有两种方法可以执行此操作,而无需使用subprocess
,
os
模块,
import os
filesInDir= [ i for i in os.listdir("/path/to/dir") if i != "yourFile.txt" if i! = "yourFile.txt2" ]
for i in filesInDir:
os.remove(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.