IronPython: File Extension using FileSystemInfo

Hi,

In a monitor submission script, I want to check the file extension using .NET instead of a Python OS method (as not all systems have Python installed)…but it’s not working!
Could someone have a look at the .NET code?

[code]from System.IO import *

def SubmitButtonPressed( *args ):
global scriptDialog

List of Files to process…

Files = StringUtils.FromSemicolonSeparatedString( scriptDialog.GetValue( “Files” ), False )

extList = [".123", ".txt", ".log"]
for File in Files:
	if( not FileSystemInfo.Extension( File ) [1] in extList ):
		scriptDialog.ShowMessageBox("File Format %s NOT supported!" % File, "Error" )
		return[/code]

Thanks,
Mike

FileSystemInfo.Extension is a property, not a static function. You would need an existing instance of FileSystemInfo, like so:

fileInfo = FileInfo( File )
if( not fileInfo.Extension in extList ):
    ...

The reason I’m using FileInfo is because FileSystemInfo is abstract and can’t be instantiated. However, FileInfo subclasses FileSystemInfo, which is why the code above works. That being said, you could just use the Path.GetExtension function:
msdn.microsoft.com/en-us/library … nsion.aspx

This will probably be a little quicker, since you don’t need to hit the file system to get the results:

if( not Path.GetExtension( File ) in extList ):
    ...

Cheers,

  • Ryan

Thanks for this info!

if( not Path.GetExtension( File ) in extList ):

works well and is really fast :slight_smile:

Mike