Skip to content

Instantly share code, notes, and snippets.

@alixaprodev
Created July 3, 2022 04:49
Show Gist options
  • Save alixaprodev/1ad4b3fc25a6b48c12160986983c5526 to your computer and use it in GitHub Desktop.
Save alixaprodev/1ad4b3fc25a6b48c12160986983c5526 to your computer and use it in GitHub Desktop.
Delete all similiar files from a directory and sub directory in python
import os
def get_filepaths(directory):
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
if (filepath.endswith('.srt')):
file_paths.append(filepath) # Add it to the list.
return file_paths # Self-explanatory.
# Run the above function and store its results in a variable.
full_file_paths = get_filepaths("D:\Academic_Videos\Django Project")
for path in full_file_paths:
os.remove(path)
print (full_file_paths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment