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"
#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


Messages In This Thread
I still have little problems with my script - by kavindu - Jul-06-2018, 06:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 504 3 hours ago
Last Post: AdamHensley
  Regex to catch what user has put in text box robertkwild 10 429 Jun-13-2024, 05:44 PM
Last Post: deanhystad
  Reading an ASCII text file and parsing data... oradba4u 2 216 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,374 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  speed up getting embedding from bert model for large set of text veda 7 437 May-27-2024, 08:28 AM
Last Post: Pedroski55
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 405 May-03-2024, 07:23 AM
Last Post: Pedroski55
  UART write binary code trix 3 477 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,444 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 418 Apr-22-2024, 01:15 PM
Last Post: snippsat
Brick Number stored as text with openpyxl CAD79 2 746 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