70 likes | 205 Vues
This overview delineates the key differences between primary and secondary storage, emphasizing RAM's volatile nature versus a hard disk's non-volatile characteristics. It explores the role of file systems as a subsystem of the operating system, illustrating how programming languages like Python interface with these systems through file operations. The tutorial covers essential file methods such as open, read, and write, providing syntax examples and practical use cases for managing file input and output in Python. Enhance your programming skills by mastering file handling techniques!
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)