Python Forum
How to write a code to open and read text file by clicking on the "text file"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write a code to open and read text file by clicking on the "text file"
#1
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
Reply
#2
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.

  1. Associate the extension .txt to 'py C:\windows\your_script.py'
  2. 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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Python doesn't care what the file is named, if it contains text, as does '.py' files, that means you can perform text base instructions on it.

Here's a good writeup: https://hackernoon.com/determining-file-...e7b18d4fc4
Reply
#4
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 Cry
Reply
#5
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/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex to catch what user has put in text box robertkwild 25 997 Yesterday, 11:29 AM
Last Post: robertkwild
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 815 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Reading an ASCII text file and parsing data... oradba4u 2 258 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,466 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  speed up getting embedding from bert model for large set of text veda 7 491 May-27-2024, 08:28 AM
Last Post: Pedroski55
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 448 May-03-2024, 07:23 AM
Last Post: Pedroski55
  UART write binary code trix 3 517 Apr-28-2024, 04:57 PM
Last Post: deanhystad
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 1,553 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  What does .flush do? How can I change this to write to the file? Pedroski55 3 451 Apr-22-2024, 01:15 PM
Last Post: snippsat
Brick Number stored as text with openpyxl CAD79 2 838 Apr-17-2024, 10:17 AM
Last Post: CAD79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020