70 likes | 198 Views
Files in a Nutshell. Primary Vs. Second Storage. My Computer 8 GB RAM 1 TB Disk space Ram access time: nanosecond range Disk access time: millisecond range Ram is volatile Disk is not volatile. File Systems. The file system is a subsystem of the OS
E N D
Primary Vs. Second Storage • My Computer • 8 GB RAM • 1 TB Disk space • Ram access time: nanosecond range • Disk access time: millisecond range • Ram is volatile • Disk is not volatile
File Systems • The file system is a subsystem of the OS • All requests to read/write files are handled are passed through to the OS • A programming language—like Python— provides an interface to OS file system services
Python File Servicesopen/close • Open • Tells the OS to return information about a particular file to the program • Tells the OS whether you want to read or write • Close • Tells the OS you are finished with a file • OS writes data from memory to secondary storage
Open/Close Syntax • myFile = open(“fileName.txt”, “r”) • myFile = open(“fileName.txt”, “w”) • myFile.close()
Reading • Using the range function: >>myFile = open(“fileName”,”r”) >>for aline in myFile … • Read the entire file as a string >>myString = myFile.read() • Read n characters as a string >>myString = myFile.read(n) • Read a line into a string: >>myString = myFile.readline() • Read n lines into a list of n strings: >>myList = myFile.readine(n)
Writing • Adds a string to the end of a file. Creates the file if it does not already exist >> myFile.write(myString)