How to make a program that can be opened with a file? (python)

Joe Mother

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!

Imperishable Night

"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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make an EXE file from a Python program?

How can one prevent a program from being opened with Python?

How can I make an autorun to make a program open a file automatically

How to make sure file exists ant it is opened

Can I make an app which has a python program file in it?

How can I make a program overlay text ON TOP of a PostScript file?

How can I make specific file extension open in my program

How do I make sure a file opened by URLLoader is closed so I can write on it with a FileStrewam

How to make this program in python?

How do I make my python program to write a new file

How to make a custom file extension for your tkinter program python

How to choose default program to a file to be opened with same exe name?

How to set a single file on the desktop to be opened with a program other than the default?

How to emulate file opened in text mode in Python

how to check file is opened from python scrip

How to get line number in file opened in Python

Python how to erase a file once it's opened with "with"

How to open an already opened file in python?

How to run currently opened file in python tkinter?

How to open and edit a file with .pas format which can be opened by Text editor or Notepad++ in Python?

How to close program in python opened by os.system()?

How to make notepad++ start with a blank new file when opened?

How to make automatic tick on checkbox in Excel if the file is opened in Macro

How to make vim always show the encoding of current opened file?

Flutter: How can I make a drawer opened at the start?

Batch File to Check File Opened by Program

How can I read the last change even if the program is closed and opened in the differences in the flutter program?

Can I access an already opened file explorer via a Python Script

How can i make sure that if i closed the program and run again it will create the new file in a new directory?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive