Open the IDLE text editor that comes with the Python programming language. The IDLE program is located in your Program Files (or Applications for Macintosh) within the Python directory. A blank source code file opens in the IDLE text editor window.
File input and output is a very important function in programming languages. The Python programming language is capable of reading text from a text file on your computer. This is very useful for storing configuration settings and data for your program. Instead of entering configuration data every time you execute your Python script, you could have it read a configuration file and fill in the settings from that. Learning file input is an important step as a programmer. You can learn how to read a text file using Python in just a few minutes.
Things You'll Need
- Computer with Python 3.2 programming language installed (see Resource)
Declare a string variable that holds the path to the text file you want to read in Python. For example, if you have a text file named "etc.txt" on your C: drive, you can write the following to declare the string:
strPath = "C:\etc.txt"
Open the file using the open() function, like this:
f = open(strPath)
Read the contents of the file using the read() function. The output of the read() command can be stored in a string for printing out later, like this:
strText = f.read()
Print out the contents of the file by printing the strText string, like this:
Run the program by pressing the "F5" key. The program will print the entire contents of the text file to the console.
How to Install Python for Windows
Python is an object-oriented computer programming language which many programmers use for software development. Python code is easy to maintain and relatively...
How to Send a Cell Phone a Text Message With a Python Script
An SMS message, also known as a text message, is essentially an email sent to a mobile phone. The capacity to communicate...
How to Open a File in Python
Before the advent of databases, web services and other exotic forms of data storage, there was the file. Files will always be...
How to Read and Write Files in Python
When you're programming in the Python language, beyond the most trivial programs, you'll be required to read data from and write data...