Download presentation
Presentation is loading. Please wait.
Published bySilvia Dickerson Modified over 6 years ago
1
Files Handling In today’s lesson we will look at:
the need for permanent storage methods for persistent storage of data methods for reading from and writing to files
2
Types of Storage Primary storage (memory/RAM):
Volatile - i.e. contents are lost without power Use to store data while it is being processed Fast - access time measured in nanoseconds for PCs Relatively expensive - 1Gb DDR RAM costs about £20 Secondary storage (e.g. hard disc): Non-volatile, usually magnetic media - data are stored without power Used for permanent storage of data after it has been “saved” Slower - access time of around 8ms Cheaper, e.g. 1Tb for £50
3
Storing Data When writing a program, we use variables to store data in primary storage. What sort of things might we want to store permanently? previous activity – e.g. high scores, data collected from sales, experiments, etc. reference materials – e.g. dictionaries for spelling checkers, lists for validation, television program guides, address lists, etc.
4
Permanent Storage How can a computer store data permanently?
databases: the best way to store large amounts of data, especially if you will want to sort or search through it cookies: web-pages aren’t allowed to access your normal files for security reasons, but they can store small amounts of data in special files called cookies. files: programs can store data in files that are visible to your operating system – either in an unstructured way for small amounts of data, or using a format such as CSV or XML.
5
Access Files in Python The first stage in handling file contents in Python is to open the file. There is actually a file variable type in Python that we can use to point to a file, e.g f = open(“my_file.txt”, “r”) will open a file called my_file.txt. The second part is called the mode tells Python how to access the file – e.g. “r” means open the file read-only.
6
File Access Modes The most common file access modes are:
“r” = read only “w” = write to the file (and replace the contents) “a” = append (i.e. add to the end of the file) You can also add a + to the end of the mode if you want to be able to update the file, e.g f = open(“my_file.txt”, “r+”) Files should be closed when you have finished, e.g. using the command f.close()
7
Writing to Files If you open a file with mode w or a then it will be created if it doesn’t already exist. You can store a string in a file using the write() method, e.g f.open(“my_file.txt”, “w”) f.write(“Hello”) f.close() You can write the contents of a variable, but if it’s not a string it will need to be converted first using str().
8
Reading the Whole File You can read the whole file into a list, e.g f.open(“my_file.txt”, “r”) contents = f.read() f.close() This will create a string called contents containing every character in the file. You can split the string using the .split() method, e.g. for a CSV file you could use contents.split(“,”) to separate the string into individual values at the commas.
9
Reading the File into a List
You can read the whole file into a list, e.g f.open(“my_file.txt”, “r”) contents = f.readlines() f.close() This will create a list called contents containing each line from the file as an item in the list. The newline characters will also be read from the file and will appear in the list as \n. You can remove these characters using the strip() method, e.g. contents[0].strip()
10
Reading a Single Line Alternatively, you can read individual lines from the file into a string using readline(). If you reach the end of the file, readline() returns an empty string (i.e. nothing, or “”), so you can do things like this: f = open('my_file.txt','r') item = True while item: item = f.readline() print item.strip() f.close()
11
Further Reading... These are only the basic and most common things that you might want to do with files. You can also, for example: store non-text data (e.g. for images or sound) in files; this is called binary mode jump to a certain position inside a file, e.g. to insert data at a particular point look where you are in a file – e.g. a numbered byte read a single byte from a position in a file use pickle to store data structures (e.g. lists) in text files
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.