Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python drag and drop, get filenames [closed]

I have a folder with several spectra in it. And I wrote a program which fits Lorentzian absorption peaks to the spectra. For this the Python scipt must be in the same folder and then it reads its own path and detects the spectrum files. After doubleclicking the scipt it fits the peaks to all spectrum files in the folder.

Now I want to improve the program a bit. I don't want to have the script file in the same folder as the measurement data, and I want other people to be able to use the program easily. For this I need a method with which I can drag and drop the spectrum files into a window or similar. The scipt should only read the path and the filenames of the files. Which I can implement in my working script.

What is the best approach for such a program?

like image 885
FrankTheTank Avatar asked Mar 09 '14 11:03

FrankTheTank


1 Answers

If you don't need a GUI, and depending on the platform I would use sys.argv.

In windows for example you can't drag files onto python scripts, but you can drag them onto a batch file. And from the batch file you can call your script with the file-names as arguments.

Batch File:

python "dropScript.py" %*
pause

The %* contains all the filenames.

dropScript.py:

import sys

file_paths = sys.argv[1:]  # the first argument is the script itself
for p in file_paths:
    print(p)

The first argument is the script itself so it is omitted from the file_path list.

like image 167
BoshWash Avatar answered Nov 10 '22 18:11

BoshWash