Python: A way to detect a filetype attached to a string? -


in ironpython 2.6*, i'm trying build function "corrects" string; have 2 arguments, file , extn. idea them concatenated necessary later in program, know people don't read instructions , you're bound have enter "file.*" file, mess up.

i'm looking way take file, have function detect , strip .* (any extension of length) file if .* exists; doesn't need in string, , user entering same extension extn**, needs not prepared, merely consistently stripped.

my current method has me passing file , extn separately, it's not inconceivable redo things take file.extn , break file , extn if need be; don't want if don't have to, though, program built around former system.

*a note regarding ironpython 2.6; i'm trying avoid ironpython-specific codes , use simple of ones possible, unix-win cross-compatibility's sake. far, i've done works in python 2.7 ide's, not work in python 3.x

**a note regaring extn; want users enter proper extension extn too, know, can't sure of , method stripping .i file must not automatically include extn part of it.

here snippet of code may reference have far. file , extn variables have been added, , in practice, pulled in through middle-man program xml file script @ run-time.

file = "test" path = "c:\\" extn = ".txt"  def checkcorrect_file(srcfile): #check-corrects file     #meh, got nothin'...  def checkcorrect_path(srcpath): #check-corrects path     if srcpath.endswith('\\') == false:         srcpath = srcpath + "\\"     else:         srcpath = srcpath     return srcpath 

you can using os.path.splitext. following remove extension if 1 exists (and nothing if doesn't):

import os file = os.path.splitext(file)[0] 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -