Calling rm from subprocess using wildcards does not remove the files

后端 未结 3 1321
有刺的猬
有刺的猬 2020-12-15 07:29

I\'m trying to build a function that will remove all the files that start with \'prepend\' from the root of my project. Here\'s what I have so far

def cleanu         


        
3条回答
  •  北海茫月
    2020-12-15 07:53

    Would you consider this approach using os.remove() to deleting files instead of rm:

    import os
    os.remove('Path/To/filename.ext')
    

    Update (basically moving my comment from below into my answer):

    As os.remove() can't handle wildcards on its own, using the glob module to help will yield a solution as repeated verbatim from this SO answer:

    import glob
    import os
    for fl in glob.glob("E:\\test\\*.txt"):
        #Do what you want with the file
        os.remove(fl)
    

提交回复
热议问题