954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with a simple script to automate batch file generation

Hi folks! This is my first python script! It will be executed within AvsP, which is an application for writing AviSynth scripts. It provides a full Python interface, for macros and other operations.

Here's the code I have now:

# batchCreateD2Vs
# by Derek Prestegard
# Last Modified 4/3/08
#
# This python script works through a directory, and generates a d2v for all
# VOB and MPG files encountered. It will not extract audio, and it will 
# honor pullown flags

# Changelog:
# r001 4/3/08 - 4:47 PM - Initial script

import os
import fnmatch

# Get the directory containing source files
dirname = avsp.GetDirectory()

# Create a BAT file
f= open('batchDecode.bat', 'w')

if dirname:
    # Generate each of the d2v files
    for filename in os.listdir(dirname):
        fullname = os.path.join(dirname, filename)
        if os.path.isfile(fullname):
            # Only work on VOBs and MPGs (for now)
            if fnmatch.fnmatch(filename, '*.vob'):
               # Build the DGIndex command string
               indexStr = 'dgindex.exe -IF=[' + fullname + '] -FO=0 -OM=0 -OF=[' + fullname + '] -MINIMIZE -EXIT \n'
               # Write the string to a BAT file
               f.write(indexStr)
            if fnmatch.fnmatch(filename, '*.mpg'):
               # Build the DGIndex command string
               indexStr = 'dgindex.exe -IF=[' + fullname + '] -FO=0 -OM=0 -OF=[' + fullname + '] -MINIMIZE -EXIT \n'
               # Write the string to a BAT file
               f.write(indexStr)
f.close()


I know, its ugly :) I'm no programmer. It's complaining about line 26, which is:
if fnmatch.fnmatch(filename, '*.vob'):

From my understanding, this line should evaluate whether "filename" ends in .vob.

I want my script to operate on VOBs and MPGs, so if I can combine that into a single IF statement, that would be ideal.

I welcome your input!

Thanks,
~MiSfit

blue_misfit
Newbie Poster
1 post since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
I know, its ugly :) I'm no programmer. It's complaining about line 26, which is: if fnmatch.fnmatch(filename, '*.vob'):

And what does he say (Which exception is thrown) ?

jice
Posting Whiz in Training
225 posts since Oct 2007
Reputation Points: 64
Solved Threads: 57
 

Use endswith. fnmatch is specific to *nix (I think) and so won't work if you are using MS Windows.

if os.path.isfile(fullname):
     # Only work on VOBs and MPGs (for now)
     if filename.endswith('.vob') or filename.endswith('.mpg'):
         f.write('dgindex.exe -IF=[%s] -FO=0 -OM=0 -OF=[%s] -MINIMIZE -EXIT \n' % \
                   (fullname, fullname))
##
##   You could also slice off the end if you have a lot of different endings
ending_tuple=( ".mpg", ".vob" )
ending = filename[-4:]
if ending in ending_tuple:
     ##write to file
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You