0

I have a list of filepaths in a master file

/home/tmp/dir1/file1.xml
/home/tmp/dir2/file2.xml
/home/tmp/dir2/file3.xml

I am trying to extract the names like "file1", "file2", "file3", without the .xml or the leading filepath

So in my code, I am doing something like:

for line in masterfile:
    fname = line.rsplit('/', 1) //e.g : [/home/tmp/dir1, file1.xml]
    fname_noext = (fname[1]).rsplit('.xml')
    print fname_noext[0]

I keep getting

fname_noext = (fname[1]).rsplit('.xml') IndexError: list index out of range

Any idea what I might be doing wrong? I checked for trailing white space etc, doesn't look like there is anything there.

ExceptionHandler
  • 213
  • 1
  • 8
  • 24

1 Answers1

2

try to leverage os lib

import os
for line in masterfile:
    fname = os.path.basename(line )
    fname_noext, _ = os.path.splitext(fname )
    print fname_noext
galaxyan
  • 5,944
  • 2
  • 19
  • 43