Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to open file
#1
Hi
the code is
 print("a file")
text_file = open('123.txt','r')

print(text_file.read())

text_file.close()
but its not opening the file. just showing [Finished in 0.2s] in console. I am using sublime text
Reply
#2
FYI: print statement is indented 1 character, This should give you a syntax error
Reply
#3
are you sure 123.txt is not an empty file with nothing in it?
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
#4
there is no syntax error.

Yes 123.txt is not empty file and its placed in the same folder as python file.

This python settings is a headache more difficult than understanding code
Reply
#5
when I run your code as posted, it produces a syntax error (as it should with line 1 indented 1 character)
Is the posted code exactly what you are running
modify code as follows and try again:
import os

# make sure running in proper directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))

print("a file")
text_file = open('123.txt','r')
 
print(text_file.read())
 
text_file.close()
better:
import os

# make sure running in proper directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))

print("a file")
with open('123.txt','r') as fp:
    print(text_file.read())
Reply
#6
i think there is nothing wrong with code because all the suggested codes are giving same output [Finished in s]. There may be something i didnt installed properly or have some issue with sublime text editor i am using
Reply
#7
What happens if you run the script with a regular shell rather than Sublime?
Reply
#8
it immediately print
 print("a file")
:D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 314 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,087 Dec-14-2023, 08:03 AM
Last Post: shanoger
  How can i combine these two functions so i only open the file once? cubangt 4 852 Aug-14-2023, 05:04 PM
Last Post: snippsat
  I cannot able open a file in python ? ted 5 3,286 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,357 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,398 May-06-2022, 01:44 PM
Last Post: Larz60+
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,020 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Unable to upload file using FTP korenron 2 2,478 Dec-23-2021, 12:44 PM
Last Post: korenron
  Open an excel file Newbie1114 1 2,330 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  How to open MIDI-file and get events in a list? philipbergwerf 7 4,958 May-29-2021, 08:24 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

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