I'm a windows user. The notepad is the default program for.txt files. I need to write a code to open and read a text file by clicking on the text file when the python file has been selected as the default program for the text files.How to do that
python v3.7
You want to associate text files extension with a Python program?
This is possible, but I think it's not a good idea to open all text files generally with Python.
- Associate the extension .txt to 'py C:\windows\your_script.py'
- Clicking now on a .txt file should call following: 'py C:\windows\your_script.py filename.txt'
Then you read with
sys.argv[1]
the first argument from the commandline.
sys.argv[0]
is the python-script.
Then you can do what you want with the path.
Thank you
Dead_Eye. I wrote a code just as he said and
I selected my python script as the default for txt files.
this is my python code
import sys
file=open(sys.argv[1],"r")
print(file.read())
When I dragged a txt file and dropped it over my python script, the python script reads the text file successfully.
But when I double clicked on the txt file, then it shows following error msg
Error:
C:Users\Kavindu\Documents\python_workspace\a2.txt is not a valid Win32 application
What is the reason for this error msg, and how to solve this problem.
I need to open txt files with my python script by clicking on text files
plz can someone help me with this

DeaD_EyE provided one option, but also pointed the drawback
one alternative would be to add entry (shortcut to your python script e.g
c:\python37\python.exe c:\path_to\yourscript.py
) in the
Send To
right-click menu. So you can right click on the text file and select SendTo->your python script.
https://www.howtogeek.com/howto/windows-...d-to-menu/