繁体   English   中英

使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM