April 2017
Beginner to intermediate
312 pages
7h 23m
English
Let's create a simple text file, read_file.txt with the following text: I am learning Python Programming using the Raspberry Pi Zero and save it to the code samples directory (or any location of your choice).
To read from a file, we need to make use of the Python's in-built function: open to open the file. Let's take a quick look at a code snippet that demonstrates opening a text file to read its content and print it to the screen:
if __name__ == "__main__": # open text file to read file = open('read_line.txt', 'r') # read from file and store it to data data = file.read() print(data) file.close()
Let's discuss this code snippet in detail:
Read now
Unlock full access