Python Tutorial : CHAPTER 6 Reading .txt files
It is a very simple task in Python. You can even read a lot of .txt files simultaneously but by the meantime we are going to see a small sample about how to open and read an external .txt file. No matter where is located the file you could read it and display the content on screen through Python interpreter or by writing a program in Python (*.py).
1. Firstly, we are going to create a file containing the following information inside.
NEWS WEBSITES:
A. www.bbc.com
B. www.nyt.com
C. www.reuters.com
D. www.listin.com.do
E. www.nbc.com
2. Copy this list of news websites in a *.txt file and save it in the directory you want. If you want save it in
C:\Python25\newswebsites.txt
3. The next step is to write the following code in a new window and save it to read this program: Define a variable to read the file.
This variable could be “R”. Then you should say that “R=Open (“nameofile.txt”). We established that the new of the file is newswebsite.txt. Then you must have to create a new variable which could be named “file”. Then you must have to say that “file = R.read()”. At last, you should print the variable “file”. The save the program with this name: “newswebsites.py”. Remember that this file should be saved in the same directory you have the *.txt file and ready to go.

