0

When reading from fileChooser.selection I get a nearly correct path to the choosen file, but with one additional folder in the path that does not exist. It seems like fileType is added after 'MY_GUI_KIVY'. What am I missing here?

The code is part of a GUI that should help manage projects. To load the related file I need to get the path to that file. After trying to find the problem on my own, searching the net to find similar problems and eventually adopt their given solutions, I could not find a working solution.

def loadFile(self, fileType):
    self.getPath(fileType)
    # code shortened for better readability
    return

def getPath(self, fileType):

    # create popup layout
    content = BoxLayout(orientation='vertical', spacing=5)
    popup_width = 500
    self.popup = popup = Popup(title='Open ' + str(fileType), content=content, size_hint=(None, 0.9), width=popup_width)

    # create the filechooser
    initDir = 'projects'
    if not fileType == 'project':
        initDir += '\\' + fileType
    initDir = initDir + '\\'
    textinput = FileChooserListView(path=initDir, size_hint=(1, 1), dirselect=False, filters=['*.' + fileType])
    self.textinput = textinput

    # construct the content
    content.add_widget(textinput)

    # 2 buttons are created for accept or cancel the current value
    btnlayout = BoxLayout(size_hint_y=None, height='50dp', spacing='5dp')
    btn = Button(text='Ok')
    btn.bind(on_release=partial(self.loadSelectedFile, fileType, textinput, popup))
    btnlayout.add_widget(btn)
    btn = Button(text='Cancel')
    btn.bind(on_release=popup.dismiss)
    btnlayout.add_widget(btn)
    content.add_widget(btnlayout)

    # all done, open the popup !
    popup.open()
    return

def loadSelectedFile(self, fileType, fileChooser, popup, *args):  # *args ist unnötig, aber da die partial den Button auch noch zwingend mitliefert...
    log.debug(str(fileChooser.selection))
    # code shortened for better readability
    return True

Sorry, I'm not allowed to post images yet. Link should show the Image. Shows order structure - I had to blur some folders but they are not linked to the problem

If I set fileType to 'project', the output is something like that: 'C:\GUI\MY_KIVY_GUI\projects\projects\test.project'

The expected path should have been: 'C:\GUI\MY_KIVY_GUI\projects\test.project'

If fileType is set to 'subproject', the output looks like this: 'C:\GUI\MY_KIVY_GUI\projects\subproject\projects\subproject\test.subproject'

The correct path should have been the following: 'C:\GUI\MY_KIVY_GUI\projects\subproject\test.subproject'

So it seems like initDir is added after 'MY_KIVY_GUI' but I don't get why this happens.

Sandro
  • 25
  • 1
  • 7
  • It looks like you are adding `fileType` at the line `initDir += fileType + '\\'`. Try putting some `print` statements there to see what is actually happening. – John Anderson Aug 19 '19 at 13:07
  • in the meantime, I played a little bit around and recognised, that I made a little mistake with my outputs in my question. Pls take another look at them, I'll edit that in a moment. It seems that it's not fileType but initDir, whats added in the path. Concerning your idea, I know exactly what happens there but checked it anyways: With 'project' as fileType initDir is 'project\\' afterwards. With 'subproject' initDir is 'project\\subproject\\' just as expected and it should be. – Sandro Aug 19 '19 at 13:32
  • Without a [mcve] and your folder environment, we can only make guesses and suggestions. Do the incorrect outputs actually exist in your folders? Are you 100% sure that you are not unintentionally selecting the "incorrect" folder in the `FileChooserListView`? – John Anderson Aug 19 '19 at 14:26
  • I am 100% sure not to select the wrong folder. When I try to open the file at this path, an error occurs that says 'file not exists' while if I pass the correct path manually the file opens without any problems. Its too late today for a minimal reproducible example, but I'll make one tomorrow. I'll try to add the filestructure in a few minutes. – Sandro Aug 19 '19 at 14:36
  • Hope the image shows enought. There are no other folders beneath as you can see. I 'renamed' the folders like vehicle, norm and track in my question to 'subproject' to keep the example simple. The output behaves the same in each case. – Sandro Aug 19 '19 at 14:53

1 Answers1

0

I see the same behavior that you described. I think you may have discovered a bug in the FileChooser code. I think your code should work as written. However, a work around is to add the line:

initDir = os.path.abspath(initDir)

just before you create the FileChooserListView. This uses an absolute path instead of a relative path, and seems to work.

John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • Solves my problem. I think I'll try to contact the developers to inform them about this bug. Thx for helping me out here! – Sandro Aug 20 '19 at 06:26