1

I'm trying to implement "drop files here" feature in a simple GUI application using tkinter in python

It uses the TkinterDnD2 which is ended up by seeing this stackoverflow answer

Upon dropping the file event.data returns file name surrounded with curly braces

Example: {d:/test/sample1.pdf}

However when multiple files are dropped in the target each path is surrounded by curly braces

Example: {D:/test/sample1.pdf} {D:/test/sample2.pdf}

How to properly parse the file paths from this string?

Community
  • 1
  • 1
Divakar Rajesh
  • 1,140
  • 2
  • 18
  • 23

2 Answers2

1

This can be easily achieved using this two lines of python code:

temp = filePathsReturned[1:-1]
list = temp.split("} {")
Divakar Rajesh
  • 1,140
  • 2
  • 18
  • 23
  • 1
    Yeah that works well carry on. My answer works well if there are no gaps between paths and as well there are multiple {} curly braces as highlighted in my answer – Naseer Mohammad Aug 19 '18 at 06:22
  • 1
    actually this answer is only a derivative from your answer..thank you so much for providing the insight – Divakar Rajesh Aug 19 '18 at 06:35
0

I'm not sure whether TkinterDnD2 library will return a stream of paths in a single string or single path in a string.

If it is single path in a string the solution is simple:

>>> str1="{mypath/to/file}"
>>> str1[1:-1]

The output would be

'mypath/to/file'

But I suppose your problem is multiple paths coming in single string. You can implement code as below.

Consider str1 to be string with multiple paths:

str1="{mypath1/tofile1}{mypath3/tofile3}{mypath2/tofile2}"
i=0
patharr=[]
while(str1.find('{',i)>=0):
    strtIndex=str1.find('{',i)
    if(str1.find('}',strtIndex+1)>0):
        endIndex=str1.find('}',strtIndex+1)
        patharr.append(str1[strtIndex+1:endIndex])
        print(str1[strtIndex:endIndex+1])
        i=endIndex+1
print(patharr)

Now the output would be as follows:

['mypath1/tofile1', 'mypath3/tofile3', 'mypath2/tofile2']

So as per your question in comment below is code for file paths which are having curly braces with in the file paths.

str1 = "{mypath}}}}1/tofil{{{e1}{mypath3/tofile3}{mypath2/tofile2}}}}}}"
i = 0
patharr = []
while (str1.find('{', i) >= 0):
    strtIndexfirst = str1.find('{', i)
    notFound = True
    strtIndex = strtIndexfirst
    while (notFound and strtIndex < len(str1)):

        if (str1.find('}', strtIndex + 1) > 0):
            findInd = str1.find('}', strtIndex + 1)

            if (findInd == len(str1) - 1):
                endIndex = str1.find('}', strtIndex + 1)
                patharr.append(str1[strtIndexfirst + 1:endIndex])
                print(str1[strtIndex:endIndex + 1])
                i = endIndex + 1
                break
            if (str1[findInd + 1] == '{'):
                endIndex = str1.find('}', strtIndex + 1)
                patharr.append(str1[strtIndexfirst + 1:endIndex])
                print(str1[strtIndex:endIndex + 1])
                i = endIndex + 1

                notFound = False
            else:
                strtIndex = findInd
        else:
            strtIndex = str1.find('}', strtIndex + 1)

print(patharr)

For above string output was

['mypath}}}}1/tofil{{{e1', 'mypath3/tofile3', 'mypath2/tofile2}}}}}']

If str1 is

str1="{{{mypath1/tofil}}e1}{mypath3/tofile3}{mypath2/tofile2}}}}}}"

Output is

['{{mypath1/tofil}}e1', 'mypath3/tofile3', 'mypath2/tofile2}}}}}']

Worked out for other test cases as well. This piece of code is working fine.

halfer
  • 19,824
  • 17
  • 99
  • 186
Naseer Mohammad
  • 389
  • 4
  • 14
  • I thought of doing something like this..but it might become an issue if the file name has curly braces in them.. – Divakar Rajesh Aug 15 '18 at 11:55
  • 1
    @DivakarRajesh please check now Edited and updated answer – Naseer Mohammad Aug 15 '18 at 13:21
  • This doesn't work out for `{C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility-programs-in-python/Working with PDF/sample2.pdf} {C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility-programs-in-python/Working with PDF/test.pdf} {C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility-programs-in-python/Working with PDF/sample1.pdf}` – Divakar Rajesh Aug 19 '18 at 05:41
  • 1
    There are gaps between path end } and path start {. This is not expected. This is again lot of work. May be will help you later. – Naseer Mohammad Aug 19 '18 at 06:11
  • thank you so much for your effort. I have solved it myself – Divakar Rajesh Aug 19 '18 at 06:12