Ignore folders with certain filetypes

Posted by gavin19 on Stack Overflow See other posts from Stack Overflow or by gavin19
Published on 2011-01-01T16:34:03Z Indexed on 2011/01/01 18:53 UTC
Read the original article Hit count: 257

Filed under:

I'm trying in vain to rewrite my old Powershell script found here - "$_.extension -eq" not working as intended? - for Python.I have no Python experience or knowledge and my 'script' is a mess but it mostly works. The only thing missing is that I would like to be able to ignore folders which don't contain 'mp3s', or whichever filetype I specify. Here is what I have so far -

import os, os.path, fnmatch

path = raw_input("Path :  ")

for filename in os.listdir(path):
if os.path.isdir(filename):
    os.chdir(filename)
    j = os.path.abspath(os.getcwd())
    mp3s = fnmatch.filter(os.listdir(j), '*.txt')
    if mp3s:
        target = open("pls.m3u", 'w')
        for filename in mp3s:
            target.write(filename)
            target.write("\n")
    os.chdir(path)

All I would like to be able to do (if possible) is that when the script is looping through the folders that it ignores those which do NOT contain 'mp3s', and removes the 'pls.m3u'. I could only get the script to work properly if I created the 'pls.m3u' by default. The problem is that that creates a lot of empty 'pls.m3u' files in folders which contain only '.jpg' files for example. You get the idea.

I'm sure this script is blasphemous to Python users but any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about python