Syntax and Usage

Files

Files: Reading from Files

A common programming task is to read or write information from/to a file.


Let’s first see how to read from a file:

1) Download the file source.txt from our web-page and place it into Documents folder

2) type in the following command in the Python Shell:

>>> mySource = open("source.txt")

source.txt

1

1.4

0

1.6

-4



A common programming task is to read or write information from/to a file.


Let’s first see how to read from a file:

 1) Download the file source.txt from our web-page and place it into Documents folder

 2) type in the following command in the Python Shell:
>>>mySource = open("source.txt")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
mySource=open("source.txt")

FileNotFoundError: [Errno 2] No such file or directory: 'source.txt'


3) Create a new file in Python Editor, type in the following commands and save the file in the Documents folder:

mySource = open("source.txt")
     for line in mySource:
print(line)
mySource.close()

4) Run it!>


source.txt

1

1.4

0

1.6

-4



Methods for reading from a file:


Method Description
open(<filename>) reads everything from a file and reads it as a string
close()
closes an opened file
read() reads everything from a file and reads it as a string
readline() returns a list of strings
readlines() reads one line from a file and returns it as a string
for i in f Iterates over the lines in a file (i is a line from the file)


We can replace the previous program:

mySource = open("source.txt")
for line in mySource:
    print(line)
mySource.close()
source.txt:

1

1.4

0

1.6

-4


with:

mySource = open("source.txt")
text = mySource.read()
prisnant(text)
mySource.close()
 
Python Shell:

1

1.4

0

1.6

-4



What if we want to find the sum of all numbers in a file?


source.txt

1

1.4

0

1.6

-4



What if we want to find the sum of all numbers in a file?



mySource = open("source.txt ")                
 s = 0                
for line in mySource:                
 s += float(line)                
mySource.close()                
 print("The sum of all values is" , s)
                

source.txt

1

1.4

0

1.6

-4


What if we want to find the sum of all numbers in a file?


mySource = open("source.txt ")                
 s = 0                
for line in mySource:                
 s += float(line)                
mySource.close()                
 print("The sum of all values is" , s)


The sum of all values is 0.0.



source.txt

1

1.4

0

1.6

-4


You can find this program in the file workWithFile3.py




What if we want to find the sum of all numbers in a file, but the source file is formatted differently?
- the numbers are separated by a space or by a new line.


source2.txt

1    8    9.2    -5

1.4    9    8

0    12    -23     -9 1

1.6    2.3    -9.1

-42    -91    76    23    7



What if we want to find the sum of all numbers in a file, but the source file is formatted differently?
- the numbers are separated by a space or by a new line.


mySource = open("source2.txt")
s = 0
for line in mySource:
  line = line.rstrip()
  numbers = line.split(" ")
  for item in numbers:
  s += float(item)
mySource.close()
print("The sum of all values is",s) 
                    


source2.txt

1    8    9.2    -5

1.4    9    8

0    12    -23     -9 1

1.6    2.3    -9.1

-42    -91    76    23    7


You can find this program in the file workWithFile4.py