Get path of folder

Is there any way to get the path of a folder?

system.file.openFile() makes you choose an actual file and not a directory.

Thanks!

Mike

You could get the path from the return value:

UNIX

sFileName = system.file.openFile("/initial/path/")
sPath = sFileName[:sFileName.rfind("/")]

Windows

sFileName = system.file.openFile("c:\\initial\\path\\")
sPath = sFileName[:sFileName.rfind("\\")]

But if there is no file in the folder, then I guess you’ll have a problem.

Could you give me a little bit of a description as to what you are trying to accomplish? What are you going to do with the folder path?

Bumping an old topic because I needed to accomplish the same thing as the OP and wanted to share the solution that I found for anyone else who might need it.

from javax.swing import JFileChooser

chooser = JFileChooser()
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)
chooser.setAcceptAllFileFilterUsed(False)
chooser.setDialogTitle("Select Folder")
if chooser.showOpenDialog(None) == JFileChooser.APPROVE_OPTION:
	event.source.parent.getComponent('Text Field Path').text = str(chooser.getSelectedFile())
3 Likes