6

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?

FrankTheTank
  • 1,579
  • 4
  • 14
  • 19
  • Is it really needed to create a GUI? Why not just a command line script, with the directory with the plots passed as one of its parameters? – Bas Swinckels Mar 09 '14 at 11:21
  • You can use `tkinter` to choose a directory: http://stackoverflow.com/questions/11295917/how-to-select-a-directory-and-store-the-location-using-tkinter-in-python – jonrsharpe Mar 09 '14 at 11:34
  • Possible duplicate: [Drag and drop onto Python script in Windows Explorer](https://stackoverflow.com/q/142844/3357935) – Stevoisiak Sep 19 '18 at 16:53

1 Answers1

13

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.

BoshWash
  • 5,320
  • 4
  • 30
  • 50
  • Cool this works! The program reads the filepaths of my files. Is there any sys operation with which I do only extract the file names and the file path seperatly? – FrankTheTank Mar 09 '14 at 13:12
  • [os.path.split](http://docs.python.org/2/library/os.path.html#os.path.split) can split paths and file-names – BoshWash Mar 09 '14 at 13:25
  • also os.path.dirname() returns direcories and os.path.basename() returns file-names from paths – BoshWash Mar 09 '14 at 13:28
  • Unless I'm missing some nuance in what you're saying, you can drag a file or folder onto a python script in Windows – User Sep 29 '14 at 09:11
  • @User Hmm, I doesn't work for me on Windows 7. – BoshWash Sep 29 '14 at 10:05
  • Works for me Windows 7 python 2.7.8. Here's a blog post by somebody also suggesting the same thing: http://everydayscripting.blogspot.com/2009/08/python-drag-and-drop-zip.html ... unless the issue is multiple files, haven't tried that only single file or folder. – User Sep 29 '14 at 10:26
  • @User Strange, definitively doesn't work here. Maybe it's some kind of security feature preventing it. – BoshWash Sep 29 '14 at 22:06
  • I tried @BoshWash example but the window flashes and closes very quickly so can't tell if it works. I just dragged a file called 'hello.txt' on it. Could you help so it prints the file name and not close immediately? Thanks – Helen Neely Aug 25 '17 at 11:39
  • Please ignore my last message. I just needed to add 'pause' to the batch script to stop it closing immediately. It works now :) If you could add that to your sample code above, cheers. – Helen Neely Aug 25 '17 at 13:09
  • Does this mean `filename` would need to be the second argument if you wanted to run the script from a command line? – Stevoisiak Sep 19 '18 at 16:49
  • @StevenVascellaro yes, sys.argv will contain all arguments passed to python, which includes dropScript.py – BoshWash Sep 20 '18 at 17:25
  • For me this fails because it tries to run the program in the directory of the *dropped* file, not where the bootstrap .bat file is. – Szak1 Jul 13 '20 at 14:35