Open file with 2 clicks in a Python program

Asked

Viewed 390 times

0

Hello. I’m creating a small Python notepad and wanted to know how I open a file. txt using it from 2 clicks in Windows Explorer?

2 answers

0

In the context menu go to Open With->Python and use a script like this:

file = open("text1.txt", "r")

print(file.read())

input("Press Enter to quit: ")
  • Thanks for trying to help me, but, the script needs to open along with the file while making two clicks on windows explorer, type, files. docx, when giving 2 clicks on it the word opens showing the document. In your example, the file to be opened must be predetermined

  • With an . bat file it is possible to use drag and drop to open a file dynamically.

0


You can create a file . bat calls python and associate the . txt files to your program . bat.

bat file.

python programa.py %1

This way is passed the name of the file you opened is passed as argument.

To get the file name just use argv

import sys

sys.argv[0] # Sempre o caminho do programa
sys.argv[1] # O primeiro argumento, no seu caso o arquivo aberto.

I hope I’ve helped.

@gigafleet

Browser other questions tagged

You are not signed in. Login or sign up in order to post.