ttimti
ttimti

Reputation: 1

Print file name in jython

let say the path is Desktop\Chem\test.png i want to print the name of the file without the .png this is my code

 def test():
  file=pickAFile()
  shortFile=getShortPath(file)
  end = shortFile.split('\\')[1]
  print"this is a",end

so the solution would be "this is a test" instead of "this is a test.png"

Upvotes: 0

Views: 520

Answers (1)

Foon
Foon

Reputation: 6468

First off, you should probably be using os.sep instead of an explicit \ (so this will work on windows, linux, OS-X, etc. instead of just windows, but better yet, in this case, use os.path.splitext and os.path.basename (as documented in the jython docs which appear to exactly match the python equivalent

something like:

import os
def test():
  file=pickAFile()
  shortFile=getShortPath(file) #assume this returns c:\foo\bar\test.png
  basename = os.path.basename(shortFile) # returns test.png
  end,ext = os.path.splitext(basename) # this returns test,png
  print"this is a",end,"which is a",ext,"file"

Upvotes: 1

Related Questions