大家好我想要删除特定文件夹上的所有文件/文件夹,为此我编写了以下代码:(我想删除co_directory中保存的目录中的所有文件/文件夹,但packages_with _....除外txt文件然而我收到了错误
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是全局变量**
答案 0 :(得分:1)
有两种方法可以做到这一点,而不使用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)