1

I was making a program that opens a file and does a few things to it, and I wondered if there was a way that you could click on a file, and it would open it in the program, instead of going into the program, clicking open, and navigation through the files to find it, or just a way where you can click "Open With..." and select your program. Here is the code if it helps:

from tkinter import *
from tkinter import filedialog
from subprocess import *
import os

root = Tk()
root.title("Snake converter")
def open_file():

    filename = filedialog.askopenfilename(filetypes = (("Snake files", "*.sim"),("Python Files", "*.py"),("All files", "*.*")))
    filenametmp = filename + ".tmp"
    print filename + " is being compiled. Please wait..."
    tf = open(filenametmp, "a")
    f = open(filename, "r")
    filecontents = f.read()
    tf.write("from simincmodule import *" + "\n")
    tf.write(filecontents)
    os.chdir("C:\Snake\info\Scripts")
    print os.getcwd()
    call(["pyinstaller", filenametmp])
    os.remove("C:/Snake/info/Scripts/build")
    f.close()
    tf.close()
    print "\n\n----------------------------------------------------------------------\n\nFinished compiling " + filename + ". Find the program under [filename]/[filename].exe"

openbutton = Button(root, text = "Open", width = 10, command = open_file)
openbutton.pack()

root.mainloop()

Any help or suggestion will be highly appreciated.

Thanks!

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Anonymous
  • 355
  • 2
  • 14
  • 1
    Have it ever struck you that if you want to have an alternate way to open a file, you probably should split `open_file()` into two functions, one opening the dialog and one actually opening the selected file? – Imperishable Night Nov 23 '17 at 02:45
  • On Windows it's possible task, even if you wish to do this programmatically. Create a new association for desired file extension via `assoc` command in `cmd` (`assoc .tmp=`), then bind a command for it via `ftype` (`ftype ="python " "%1" "%*"`). Obviously, you can wrap such idea with `subprocess` module. Admin privileges required. – CommonSense Nov 23 '17 at 09:31
  • @ImperishableNight Good Idea! – Anonymous Nov 23 '17 at 10:03

3 Answers3

2

"Open with..." usually sends the pathname of the file to sys.argv. So at an appropriate place of your program, add this:

if len(sys.argv) > 1:
    open_file(sys.argv[1])

(As I said in the comment, you really want to let your open_file take an argument, and have another function like open_file_dialog to open the dialog.)

That leaves the question of how to make something you can "Open with..." in the first place. If you are on Windows, you should be able to achieve finer control with file association by editing the registry: see this MSDN page for details.

Alternatively, a quick-and-dirty way to do it is to make a .bat script that takes an argument and passes it to your python program. I remember doing this some time ago, but I haven't used Windows seriously for a long time, so I can't tell you how to write the script right off my head.

Imperishable Night
  • 1,503
  • 9
  • 19
  • This proposed solution is not quite what the OP is asking for... He is looking more for "how to configure the OS to open my file automatically when I click on it." However, having said that, this is a pretty reasonable step in that direction, since the above code will let the OP drag and drop a file onto his program executable (assuming it's executable) and have it open the executable and open the file. – GaryMBloom Nov 23 '17 at 08:03
  • @Gary02127 Sorry if you mis-understood it, but that was not what I was asking though. Although Thanks! – Anonymous Nov 23 '17 at 10:21
1

I am a Fan of Autohotkey Tools And Python Launguages,

An Other Way to do it, is:

If you want to run a program, and then Want to opened it with a file ("Open With...")

You can Think about,

Making your own Computer Movements Scripts With Keyboard Shortcuts Macros.

Step 1: Install (Python27) on your Windows System. Click Here

Step 2: Then Install the Python Packages - pyautogui and pywinauto

You can Use this Msdos Batch Script:

Install.bat

C:\Python27\scripts\pip.exe install pyautogui
pause
C:\Python27\scripts\pip.exe install pywinauto
pause

Now You are Ready, To Make and use this Python Script:

Example1.pyw

#run Notepad with "Open..." 
#######################
import pywinauto
pywinauto.Application().start(r"C:\Windows\System32\Notepad.exe c:\test\test.txt")
#######################

Example2.pyw

#run Notepad
#######################
import pywinauto
pywinauto.Application().start("C:\Windows\System32\Notepad.exe")
#######################


#Open a File - "Open With..." 
#######################
import pyautogui
import time
time.sleep(2)
pyautogui.hotkey('ctrl','o') #Many Programs use Shortcut Ctrl+o to "Open With..." 
time.sleep(.750)
pyautogui.typewrite('c:\\test\\test.txt',0)
time.sleep(2)
pyautogui.hotkey('enter')
#######################

# you can send any text or Keyboard Shortcuts Combinations - Example Copy - pyautogui.hotkey('ctrl', 'c')

Note: If you use a File Path with typewrite Command - You Can Not put a ( single backslash \ ) you must replace it into a ( double backslash \\ )

Tip : Python Languages together with AutoPythonlauncher Software is a good Combination - If You Want to make, Toolbars On your Windows Desktop - Executeable Pictures with Python Scripts For Mouse or Touch Device. - For more info look at Homepage

stevecody
  • 658
  • 1
  • 6
  • 18
0

How I would do it:

  1. Make it so that your program reads from stdin and writes to stdout
  2. Use the power of the shell. If you are in a unix shell, just simply do cat infile | ./python myProgram.py > outfile

This will give the contents of infile to your program on stdin, then write the output of stdout to outfile.

setholopolus
  • 253
  • 3
  • 17
  • I think the OP is asking how he can associate certain file types with his application. That would involve converting the python script into an executable using something like cx_freeze – Jessie Nov 23 '17 at 02:46
  • 1
    Actually I think some operating systems allow associating file types with more complex command lines than an application, for example in Windows you can put `python my_app.py "%1"` in a suitable place of the registry and it will work. Alternatively, you can use a batch script, which is dirty but still works. – Imperishable Night Nov 23 '17 at 02:51
  • @ImperishableNight - if you run the Batch File the Msdos Window Will also Appears, **if you do not want that second Window** you can try this Code, Example3.pyw - import pywinauto pywinauto.Application().start(r"C:\Python27\Pythonw.exe c:\test\my_app.py") ps - Look to my answer - I Hope you Will Like this. – stevecody Nov 24 '17 at 11:15