0% found this document useful (0 votes)
39 views17 pages

File Handling - Text Files

Uploaded by

Jake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views17 pages

File Handling - Text Files

Uploaded by

Jake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

AQA A Level Computer Science

4.2 Fundamentals of data structures


4.2.1.3 File handling
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• For today…

– REVIEW: basic concepts (input/output)

– File handling
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• INPUT

• We can INPUT data in a Python program:

name=str(input(‘Please type in your name’))

age=int(input(‘Please type in your age’))

height=float(input(‘Please type in your height’))


4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• OUTPUT

• We can OUTPUT data from a Python program:

print(‘Your name is’,name)

print(‘Your age is’,age)

print(‘Your height is’,height)


4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• FILE-HANDLING - WRITING

• It is possible (and useful!) to be able to SAVE data from inside


your Python program
• This is called FILE HANDLING (METHOD 1)

text_file=open(‘sample_file.txt’,‘wt’)
text_file.write(‘This is line one \n’)
text_file.write(‘This is line two \n’)
text_file.close()
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• FILE-HANDLING - WRITING

• It is possible (and useful!) to be able to SAVE data from inside your


Python program
• This is called FILE HANDLING (METHOD 2)

filename=‘sample_file.txt’
with open(filename,‘wt’) as file:
file.write(‘This is line one \n’)
file.write(‘This is line two \n’)
# don’t need to close the file with #
this method
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
TASK 1

• Write a Python program to write the following data to a


file:

Joe Bloggs
23 Any Street
AG1 4NM

• …called address_book
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
TASK 2

• Write a Python program for a user to type in a


person’s name and address (name, street,
town, postcode), and then save that data to a
file called address_book
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
TASK 3

• Write a Python program for a user to type in


five names, and save that data to a file called
class_list
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• As well as writing to and reading from files, you
can append (‘add’) to existing files (METHOD 1)

car_db=open(‘car_database.txt’,‘a’)
car_reg=str(input(‘Type in car registration’))
car_db.write(car_reg)
car_db.close()
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• As well as writing to and reading from files, you
can append (‘add’) to existing files (METHOD 2)

filename=‘car_database.txt’
car_registration=str(input(‘Type in car number plate’))
with open(filename,‘a’) as file:
file.write(car_registration)
# again… no need to CLOSE the file if you use WITH OPEN
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• TASK 4

• Create (and save) a text file in Notepad with a class list


of student names:

• Write a Python program which will read in the name of


an extra new student (who recently joined the class)
and append the student name to the previously-saved
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• TASK 5
• Create and save a PAC-MAN (or Ms Pac-man)
high-score table using a text editor e.g.

• Write a Python program which will read in a


new high-score value and initials, and append
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• FILE-HANDLING - READING

• As well as saving to a file, we can read existing


files (METHOD 1)

text_file=open(‘sample_file.txt’,‘rt’)
print(text_file.read())
text_file.close()
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• FILE-HANDLING - READING

• As well as saving to a file, we can read existing files (METHOD


2)

filename=‘sample_file.txt’
with open(filename) as file:
whole_file=file.readlines()

for each_line in whole_file:


print(each_line)
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• We can use features of the OS (‘operating
system’) to open up some applications from
inside Python

import os
os.startfile(‘sample_file.txt’)
4.2 Fundamentals of data structures
4.2.1.3 file handling - text
• TASK 6

• Using the import os feature in Python, create


a Python program which will read in 3 names
to be stored in a .csv (spreadsheet) file

• Your program should then open the .csv


spreadsheet file in MS Excel

You might also like