Level 3 Computing Year 2 Lecturer: Phil Smith
We looked at: Previously Reading and writing files. BTEC Level 3 Year 2 Unit 16 Procedural programming
Now Now we will look at: Appending data to existing files. File Handling part 2 Using the Python OS library to rename and delete files. BTEC Level 3 Year 2 Unit 16 Procedural programming
Recap Last lesson we developed python code to create and write records to files based upon user input. We then re read the records to display them in the console. We used the file mode of r read and w write
Adding data Now we want to extend an existing file with more data. For this we need to open the file with a new mode R read W write? append
Task 1 Start pycharm Open your code file for writing and reading files. Now follow my lead.
Reading and writing files. Some of the main Python file modes. Mode r w a Description Open a file for reading only. The file cannot be changed or written to. Open a file for writing. If the file already exists, erase its contents. If it does not exist, create it. Open a file to be written to. All data written to the file will be appended to its end. If the file does not exist, create it. We shall now learn how to specify files in other folders.
Reading and writing files. If you want to open a file in a different location, you can specify a path as well as a filename in the argument that you pass to the open function. However If you specify a path in a string literal (particularly on a Windows computer), be sure to prefix the string with the letter r. Here is an example: customer_file = open(r'c:\users\phil\customers.txt', 'r')
Reading and writing files. customer_file = open(r'c:\users\phil\customers.txt', 'r') The r prefix specifies that the string is a raw string. This causes the Python interpreter to read the backslash characters as literal backslashes. Without the r prefix, the interpreter would assume that the backslash characters were part of escape sequences, and an error would occur.
Writing Data to a File Do the (file_write.py) program in the Lab07 (on the wiki) WHAT does the \n do?
Reading Data From a File Quite often a program must read the contents of a file without knowing the number of items that are stored in the file. This presents a problem if you want to write a program that processes all of the items in the file, however many there are. We know how to read lines from a file We could use a loop to read all of the items in the file, but you need a way of knowing when the end of the file has been reached.
Reading end of File In Python, the readline method returns an empty string ('') when it has attempted to read beyond the end of a file. This makes it possible to write a while loop that determines when the end of a file has been reached. Here is the general algorithm, in pseudo code: Open the file Use readline to read the first line from the file While the value returned from readline is not an empty string: Process the item that was just read from the file Use readline to read the next line from the file. EndWhile Close the file
Reading Data From a File NOTE: In this algorithm we call the readline method just before entering the while loop. The purpose of this method call is to get the first line in the file, so it can be tested by the loop. This initial read operation is called a priming read.
Reading Data flowchart Shape used for input (read) and output (write)
Using the OS with files To rename and delete files we need to use a library which contains the methods to this. The Python OS module provides methods that help you perform file processing operations, such as renaming and deleting files. Task 2 Follow my lead
Using the OS with files Other useful methods are mkdir() to create directories. chdir() to change the current directory (from the default) getcwd() to display the current working directory. rmdir() to remove directories
Summary what we have done today Appending data to files. Using files in other folders. Using the OS library to rename and delete files.
Next Time Design with pseudo code and assignment 2