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
  Sending a text from Python sawtooth500 2 115 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  write code that resides in parent directory franklin97355 3 317 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  very newbie problem on text file zapad 2 167 Apr-12-2024, 06:50 PM
Last Post: zapad
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 12 577 Apr-12-2024, 11:51 AM
Last Post: deanhystad
  help with scrolling text on RGB Matrix Foutsy 3 229 Apr-09-2024, 09:00 PM
Last Post: deanhystad
Brick Number stored as text with openpyxl CAD79 1 278 Mar-19-2024, 08:31 PM
Last Post: deanhystad
  Text parsing Arik 5 377 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  replace text in a txt cartonics 19 2,207 Jan-30-2024, 06:58 AM
Last Post: Athi
  Open/save file on Android frohr 0 307 Jan-24-2024, 06:28 PM
Last Post: frohr
  Last record in file doesn't write to newline gonksoup 3 402 Jan-22-2024, 12:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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