python - Delete files of given types IFF there are no other file types in folder -
i want delete files of type [cue,jpg,png,m3u,etc.] if , if in folder or other files of [cue,jpg,png,m3u,etc.] type. have function can me files of type [cue,jpg,png,m3u,etc.] in given folder , return list, need delete of according conditions above. give example: file q.jpg in folder itself. after myfunc() finishes, deleted.
edit sorry unclearness. let me give better example: have 2 folders, alpha , beta in folder gamma. in alpha, there 3 files: 1.mp3, 2.mp3, folder.jpg. in beta, there 1 file cover.jpg. after myfunc()
finishes, 1.mp3, 2.mp3, folder.jpg should untouched while cover.jpg should deleted.
it sounds have 2 steps:
1) given list of extensions, list of files in folder matching extension.
2) if files in directory have extension matching list, remove them
note doesn't include information how traverse directory structure, or directories test ... sample code has single directory hard-coded in.
import os dir = "mydirectory" extlist = ['ext1', 'ext2', 'ext3'] allfiles = os.listdir(dir) # files in directory myfiles = [] # appended to contain files extensiosn matching extlist file in allfiles: parts = file.split('.') # split filename based on . if parts[-1] in extensionlist: myfiles.append(file) if len(myfiles) == len(allfiles): file in myfiles: path = "%s/%s" % (dir, file) os.remove(path) os.remove(file)
Comments
Post a Comment