0

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"

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.