Files Introduction to Computing Science and Programming I
Files So far we’ve just sent output to and taken input from the user. So far we’ve just sent output to and taken input from the user. Python makes it relatively easy to write to and read from files. Python makes it relatively easy to write to and read from files. We are only going to deal with simple ASCII text files We are only going to deal with simple ASCII text files
File Output Before you can write to a file you have to open it with a line like this. Before you can write to a file you have to open it with a line like this. fileout = file(“output.txt”,”w”) Notice that you are creating an object of the type file. The “w” means that you want to write to the file. Notice that you are creating an object of the type file. The “w” means that you want to write to the file. After which you will do all of the writing to the file with statements such as this. After which you will do all of the writing to the file with statements such as this. fileout.write(“This will be written in the file\n”) After all of the writing has finished you have to close the file. After all of the writing has finished you have to close the file.fileout.close()
File Output When printing output to the screen, Python automatically adds a newline character, but we need to add these manually when sending output to a file. When printing output to the screen, Python automatically adds a newline character, but we need to add these manually when sending output to a file. fileout.write(“This is the first line.\n”) fileout.write(“This is the second.\n”)
File Output If instead of just of a bunch of regular text, you need to output more organized information such as a class roster, you can print the different pieces of info separated by commas, or some other delimiter. If instead of just of a bunch of regular text, you need to output more organized information such as a class roster, you can print the different pieces of info separated by commas, or some other delimiter.
File Output #assume nameList, idList, and yearList have been filled with data #Each line will contain the name, id, and year of one student fileOut = file(“studentInfo.txt”,”w”) for index in range(len(nameList)): fileOut.write(nameList[index] + “,”) fileOut.write(nameList[index] + “,”) fileOut.write(idList[index] + “,”) fileOut.write(idList[index] + “,”) fileOut.write(str(yearList[index]) + “\n”) fileOut.write(str(yearList[index]) + “\n”)fileOut.close()
File Output Files like the one created by the previous code are known as comma-separated value files or csv files. Files like the one created by the previous code are known as comma-separated value files or csv files. These can be taken as input by most spreadsheet programs. These can be taken as input by most spreadsheet programs. Python provides a csv module that simplifies the use of these csv files. Python provides a csv module that simplifies the use of these csv files.
File Output When you open a file to write to it in this manner, you erase everything that was in the file previously. When you open a file to write to it in this manner, you erase everything that was in the file previously. Unless you close your file, your output will not be saved. Every time you create a file object for input or output, you should call the close() method when you are finished with it. Unless you close your file, your output will not be saved. Every time you create a file object for input or output, you should call the close() method when you are finished with it.
File Input To open a file for input you create a file object in the same way as for output, except you replace the “w” what a “r” to read from the file. To open a file for input you create a file object in the same way as for output, except you replace the “w” what a “r” to read from the file. fileIn = file(“input.txt”,”r”) You can use a for loop to work with the file one line at a time. You can use a for loop to work with the file one line at a time. for line in fileIn: print len(line) print len(line) At the end you should close the file At the end you should close the filefileIn.close()
File Input Remember that every line in the file ends with a newline ‘\n’ character. This character will be included in each line that you read from a file. Remember that every line in the file ends with a newline ‘\n’ character. This character will be included in each line that you read from a file. A couple ways you can deal with this. A couple ways you can deal with this. line = line[:-1] line = line[:-1] line = line.rstrip() line = line.rstrip() This is a method that removes all of the whitespace (spaces, newlines…) from the end of a string. This is a method that removes all of the whitespace (spaces, newlines…) from the end of a string.
File Input Reading in the student info we wrote earlier Reading in the student info we wrote earlier fileIn = file("studentInfo.txt","r") nameList = [] IDList = [] yearList = [] for line in fileIn: line = line.rstrip() line = line.rstrip() name,ID,year = line.split(",") name,ID,year = line.split(",") nameList.append(name) nameList.append(name) IDList.append(ID) IDList.append(ID) yearList.append(int(year)) yearList.append(int(year))fileIn.close()
File Input The split method returns the list of parts of a string separated by the character you give it. The split method returns the list of parts of a string separated by the character you give it. name,ID,year = line.split(",") This is a shorthand you can use to assign the values of a list to multiple variables. The following code is equivalent. This is a shorthand you can use to assign the values of a list to multiple variables. The following code is equivalent. student = line.split(",") student = line.split(",") name = student[0] name = student[0] ID = student[1] ID = student[1] year = student[2] year = student[2]
Operating System When you write your program in Python you don’t have to worry about the specifics of how files are stored on disk. When you write your program in Python you don’t have to worry about the specifics of how files are stored on disk. The operating system is the piece of software that runs on a computer that communicates with the hardware. The operating system is the piece of software that runs on a computer that communicates with the hardware. Applications (including your programs) communicate with the operating system which then works with the hardware. Applications (including your programs) communicate with the operating system which then works with the hardware.
Operating System
Disks All types of disk (hard disk, floppy disk, cd, usb drive…) store information in more or less the same way. All types of disk (hard disk, floppy disk, cd, usb drive…) store information in more or less the same way. The disk is split up into blocks, often 4 kb in size, that are used for organization. The disk is split up into blocks, often 4 kb in size, that are used for organization. When writing a file that is larger than the block size, the file has to split up over multiple blocks which may not be adjacent. When writing a file that is larger than the block size, the file has to split up over multiple blocks which may not be adjacent. Of course this is all handled behind the scenes by the OS. Of course this is all handled behind the scenes by the OS.
Disks
Disks Reading a file spread over blocks like this is inefficient. The file is said to be fragmented. Reading a file spread over blocks like this is inefficient. The file is said to be fragmented. If you defragment your drive, the file will be stored in consecutive blocks, making reading of the file easier. If you defragment your drive, the file will be stored in consecutive blocks, making reading of the file easier.
Disks