Search code examples
command-linepython-idle

IDLE Error: Opens my .py file instead of the .txt data file


I'm a Python/programming novice and this is probably an novice mistake, but I've scoured the internet for answers and have found none! I'm learning on a Rosalind module that's about opening data files. http://rosalind.info/problems/ini5/ I'm pretty sure I understand everything clearly, so I'm frustrated about my inability to do this simple task.

I'm using Python 3.6.2 and IDLE. The assignment is to simply open a .txt file and read a few lines.

I downloaded the .txt file to my working directory. Then, I opened up IDLE Shell and made sure I was in the right working directory (using ls & cd). I then opened a new IDLE .py file and wrote a script:

f = open('filename.txt', 'r')

f.readlines()[2]

I saved the script as p5.py. Then, I tried to run the script by calling F5. In the Shell, I got this message:

================ RESTART: /Users/liv/Desktop/Rosalind/p5.py =================

Is that an error? I think it's just a message from IDLE that IDLE has opened p5.py. Therein lies my problem, because now I have the wrong file open.

I started realizing that when I used the Shell and called it to print, and it came back with an empty string.

What am I doing wrong?? How do I get IDLE to open the filename.txt file? ...not the .py file.


Solution

  • The RESTART line means that IDLE has sent p5.py to Python to be run, which is exactly what you want and what you asked. Python should have then opened the text file, read it, retrieved the 3rd line, and stopped. Since p5.py has no output statements and does not raise any exceptions, you will not see anything. If you change the 2nd line to

    print('line is ', f.readlines()[2])
    

    then you should see something.