开发者

Ignore folders with certain filetypes

开发者 https://www.devze.com 2023-02-02 04:29 出处:网络
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

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), '*.mp3')
    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.


If I understand correctly, your core problem is that this script is creating a lot of empty pls.m3u files. That's because you're calling open before you've even checked whether you have anything you want to write.

A simple fix would be to change this:

target = open("pls.m3u", 'w')
j = os.path.abspath(os.getcwd())
for filename in os.listdir(j):
    (title, extn) = os.path.splitext(filename)
    if extn == ".mp3":
        target.write(filename)
        target.write("\n")

into this:

target = None
j = os.path.abspath(os.getcwd())
for filename in os.listdir(j):
    (title, extn) = os.path.splitext(filename)
    if extn == ".mp3":
        if not target:
            target = open("pls.m3u", 'w')
        target.write(filename)
        target.write("\n")
if target:
    target.write("\n")
    target.write("\n")

That is, only open the file the first time we decide we need to write to it.

A more Pythonic approach might be to do something like this:

j = os.path.abspath(os.getcwd())
mp3s = [filename for filename in os.listdir(j)
        if os.path.splitext(filename)[1] == ".mp3"]
if mp3s:
    target = open("pls.m3u", 'w')
    for filename in mp3s:
        target.write(filename)
        target.write("\n")
    target.write("\n")
    target.write("\n")

That is, first create a list of the mp3s in memory (using a list comprehension here, though you could use a plain old for loop and append if you're more comfortable with that) and then open the file only if the resulting list is non-empty. (lists are truthy if non-empty)


I think you can do it in a two-level approach. First, copy the toplevel directory to another directory ignoring the directories which do not contain your mp3s files.

import shutil
IGNORE_PATTERNS = ('*mp3s')
shutil.copytree(SOURCE_DIR, TARGET_DIR, ignore=shutil.ignore_patterns(IGNORE_PATTERNS))

And then go ahead with the approach you are doing for files in your example code. Note that shutil.copytree has the ignore_patterns only from python2.5

0

精彩评论

暂无评论...
验证码 换一张
取 消