How would you delete in Python all files in directory /tmp/dir and all its subdirectories that have extension .txt or .mp3?
How would you delete in Python all files in directory /tmp/dir and all its
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You just need to use
os.walkto traverse recursively a directory andos.removewhen you find a file whose name matches your requirements.Note that
os.walkreturns on one hand file names and, on the other hand, a root directory. Hence, for theos.removeto work you’ll need to create the full filename withos.path.join.