Up until now, we dealt with very little data created by us or asked from user, and we stored them in lists or variables. In this lecture you will be introduced to file handling in Python.
After this lecture you will be able to;
A text file is a file containing characters, structured as individual lines of text. In addition to printable characters, text files also contain the nonprinting newline character, \n
, to denote the end of each text line.
Text files can be directly viewed and created using a text editor.
In contrast, binary files can contain various types of data, such as numerical values, and are therefore not structured as lines of text. Such files can only be read and written via a computer program.
Fundamental operations of all types of files include opening a file, reading from a file, writing to a file, and closing a file. Next we discuss each of these operations when using text files in Python.
All files must first be opened before they can be used. In Python, when a file is opened, a file object is created that provides methods for accessing the file.
The open
function opens the given file with r
reading access.
In [2]:
input_file = open('sample.txt', 'r') # IOError occured because we should put the write true direction.
In [11]:
input_file = open('data/sample.txt', 'r') # True direction, with folder inside.
print(input_file)
input_file.close()
When you try to print the variable created, you will not get what you want. It is just an object created to use in later statements.
To open a file the open
function used with parameter 'w'
In [9]:
output_file = open('data/mynewfile.txt', 'w') # I used data/name because I want to save in data folder
We won't get any error because we are creating a new file only error we might get is harddisk full error from system. After we are done manipulating the file we have to close the file. with close()
function.
In [10]:
output_file.close()
In [12]:
input_file = open('data/sample.txt', 'r')
empty_str = ''
line = input_file.readline()
while line != empty_str:
print(line)
line = input_file.readline()
input_file.close()
I used while
loop to show the logic behind the reading, however for
loop gives us a more elegant way.
In [13]:
input_file = open('data/sample.txt', 'r')
for line in input_file:
print(line)
In [16]:
empty_str= ''
input_file = open('data/sample.txt', 'r')
output_file = open('data/newfile.txt', 'w')
line = input_file.readline()
while line != empty_str:
output_file.write(line)
line = input_file.readline()
output_file.close()
The write
method does not add a newline character to the output string . Thus, a newline character will be output only if it is part of the string being written. But in the example above line
variable comes with \n
at the end.
The information in a text file, as with all information, is most likely going to be searched, analyzed, and/or updated. Collectively, the operations performed on strings is called string processing.
We have learned some basic operations on strings such as
name[k]
len(str)
String Traversal: The characters in a string can be easily traversed, without the use of an explicit index variable, using the for chr in string
form of the for statement.
In [17]:
space = ' '
num_spaces = 0
line = input_file.readline()
for k in range(0, len(line)):
if line[k] == space:
num_spaces = num_spaces + 1
In [18]:
num_spaces
Out[18]:
In [19]:
s = 'Hello World!'
str.isalpha()
: Returns True if str
contains only letters
In [21]:
s.isalpha() #
Out[21]:
str.isdigit()
Returns True if str contains only digits.
In [22]:
s.isdigit()
Out[22]:
In [26]:
"1".isdigit()
Out[26]:
str.islower()
and str.isupper()
: Returns True if str contains only lower/upper case letters
In [27]:
s.islower()
Out[27]:
In [28]:
s
Out[28]:
In [29]:
s.isupper()
Out[29]:
In [31]:
"HELLO WORLD".isupper()
Out[31]:
str.lower()
and str.upper()
: Returns lower/upper case version of str
In [32]:
s
Out[32]:
In [33]:
s.upper()
Out[33]:
In [34]:
s.lower()
Out[34]:
In [36]:
s # Does not change... You have to assign it to an new variable or overwrite
Out[36]:
In [37]:
s = s.lower()
In [38]:
s
Out[38]:
In [45]:
s
Out[45]:
In [41]:
s.find('d')
Out[41]:
In [43]:
s.find('x')
Out[43]:
In [46]:
s
Out[46]:
In [51]:
s.replace("l", "*")
Out[51]:
In [55]:
s
Out[55]:
In [57]:
s.strip('!')
Out[57]:
In [58]:
s
Out[58]:
In [59]:
s.strip('!').split(" ")
Out[59]:
In [62]:
s[:-4]
Out[62]:
Write a program the removes all occurrences of the letter ‘e’ from a text file. To be able to get the text file copy paste a paragraph from internet into a file and use that file as a text file. Output should be similar to this:
This program will display the contents of a provided text file
with all occurrences of the letter 'e' removed
Enter file name (including file extension): data/totc_1.txt
Th Priod
It was th bst of tims, it was th worst of tims,
it was th ag of wisdom, it was th ag of foolishnss,
it was th poch of blif, it was th poch of incrdulity,
it was th sason of Light, it was th sason of Darknss,
it was th spring of hop, it was th wintr of dspair,
w had vrything bfor us, w had nothing bfor us,
w wr all going dirct to Havn, w wr all going dirct
th othr way--in short, th priod was so far lik th prsnt
priod, that som of its noisist authoritis insistd on its
bing rcivd, for good or for vil, in th suprlativ dgr
of comparison only.
Thr wr a king with a larg jaw and a qun with a plain fac,
on th thron of ngland; thr wr a king with a larg jaw and
a qun with a fair fac, on th thron of Franc. In both
countris it was clarr than crystal to th lords of th Stat
prsrvs of loavs and fishs, that things in gnral wr
sttld for vr.
379 occurrences of the letter 'e' removed
Percentage of data lost: 6%
Modified text in file data/totc_1_e.txt