0% found this document useful (0 votes)
29 views

Files: Basic Programming Using Python

This document discusses how to read and write files in Python. It explains that files allow data to be stored permanently in secondary memory rather than just in RAM. The key commands for working with files are open, close, and write. To write to a file, you open it with open(filename, 'w'), write data using the file handle's write method, and close it with close. To read a file, you open it without the 'w'. You can then iterate line by line, stripping newlines and appending to a list. User input can select filenames, and exceptions should be handled. The activity is to write a list of names to a file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Files: Basic Programming Using Python

This document discusses how to read and write files in Python. It explains that files allow data to be stored permanently in secondary memory rather than just in RAM. The key commands for working with files are open, close, and write. To write to a file, you open it with open(filename, 'w'), write data using the file handle's write method, and close it with close. To read a file, you open it without the 'w'. You can then iterate line by line, stripping newlines and appending to a list. User input can select filenames, and exceptions should be handled. The activity is to write a list of names to a file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Files

Basic Programming using Python

Basics
So far, data for our programs are stored in the primary memory (RAM)
This means, when we exit our program, any data we entered will be
lost.
For this topic, we will learn how to make use of the secondary
memory (harddisk) to store data from our programs.

Commands to remember
open
close
write

How to write / create new files


When we open or create new files, python will give us a filehandler
which is kind of a link to the file itself.
This filehandler should be stored as a variable in our program.

How to write / create new files


To create a new file, we make use of the following syntax:
filehandler_name = open(filename,w)
Example:
fout = open(sample.txt,w)
The w stands for write. We make use of the same syntax whenever were going
to open an existing file to write on it or to create a new file if the filename does
not exist.
Remember, if the filename already exists, python will wipe out the old data!

How to write / create new files


If we want to store something to the file, we make use of the write method:
variablename = whatever_it_is_we_want_to_write_to_the_file
filehandlername.write(variablename)
Example:
line1 = Sample string sentence\n
fout.write(line1)
As youve noticed, theres a character \n at the end of the sample sentence. This is called the newline character that allows us to
write to the next line. Without this, all succeeding writes on the file will be on the same line.
When youre done with writing, use the method close to close the file:
fout.close()

How to write / create new files


We can use for loop to write into files. For example, if we want to write the elements of a list
named a_list to a file named test.txt, we can make use of the following syntax:
filehandlername = open(filename,w)
for iterationvariable in listname:
filehandlername.write(iterationvariable+\n)
filehandlername.close()
Example:
fout = open(test.txt,w)
for element in a_list:
fout.write(element+\n)
fout.close()

How to read files


We make use of the same syntax without the w:
filehandler_name = open(filename)
Example:
fout = open(test.txt)

How to read files


If we can consider a List as consisting of elements, a Dictionary consisting
of key-value pairs, we can consider a File consisting of several lines
To cycle through the lines inside the file, we make use of iterations, most
commonly the for loop.
For example, we want to store each line of the file as elements in a list
named a_list:
for line in fout:
line = line.rstrip()
a_list.append(line)

How to read files


We include the method rstrip() because of the nature of reading
files in python. When we read files, python adds an extra \n at the
end of each line we read. As a result, we end up with extra lines.
rstrip() removes those extra lines. In the code line =
line.rstrip(), what happens is that we take the value of line,
remove the extra newline character, and then assign it back to the
line variable, kinda like what happens in the code x = x+1.

Letting the user choose filenames


We can make use of the raw_input to allow users to choose their own
filenames:
variablename = raw_input(Message Prompt)
filehandlername = open(variablename)

Handle all possible errors


Use try and except to allow the program to exit gracefully (or ask
again if inside a while loop) in case the filename does not exist.

Mandatory Activity
Create a program that will store the elements of a list into a file
named sample.txt. Use your classmates names as elements in the
list. Minimum of 5 names.

You create a list first with the names of your classmates


You create a new file
You write all the elements of the list to the file
You close the file

You might also like