I use os.remove() for deleting a file, and shutil.copyfile() for copying a file. Sometimes I need to remove/copy all the files in a directory, and I use the following code.
files = glob.glob(os.path.join(profilerPath + "/*.*"))
for f in files:
os.remove(f)
It works fine, but I’d like to ask if you have better code for doing the same thing.
What about
shutil.copytree()andshutil.rmtree()? They copy/delete recursivly, i.e. everything below a given path.If you want to copy/delete files only, without traversing into subdirectories, your current solution is fine (though you should check if each file indeed is a file and not a directory — directory names could also match the pattern
*.*).