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
  needing some help to write some code for a game calculator rymdaksel 2 1,617 Mar-08-2025, 03:02 AM
Last Post: bevisandrew
  subprocess check_output text cut off Axel_Erfurt 5 788 Feb-20-2025, 02:15 PM
Last Post: DeaD_EyE
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,111 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  Python - Hidden Text / Html Mail python1337 1 2,957 Feb-08-2025, 10:47 AM
Last Post: python1337
  How to write variable in a python file then import it in another python file? tatahuft 4 953 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Problems writing a large text file in python Vilius 4 1,010 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,138 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,051 Nov-21-2024, 11:48 PM
Last Post: haihal
  parsing a tree of text first the right most aligned blocks of text and so on arvindikchari 2 790 Nov-21-2024, 01:42 AM
Last Post: BashBedlam
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,552 Oct-17-2024, 01:15 AM
Last Post: Winfried

Forum Jump:

User Panel Messages

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