1 / 17

Files in Disk Motivation

This text provides an overview of file operations in disk, including opening, writing, reading, and closing files, along with examples and syntax.

darnellp
Download Presentation

Files in Disk Motivation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files in Disk Motivation

  2. a=open("lineas.txt","w") • a: objet in memorywhichrepresentsthe file(and containsitsmaincharacteristics: location, size, cursor position, etc) • “lines.txt”: externalname of te file in disk. • The extensión .txtisnormallyusedfor files containingtext (lines of characters). • “opens” (prepares, inicializes) the file forwriting (“w”) • If file doesnotexistsiscreated, ifexistsisoverwritten • Locates file cursos at thebeginning of thespaceassignedbythesystem

  3. a.write(line+"\n") • Writes (stores) a line in the file • Writescharacteres of thestring line • Writes and end-of-line mark (specialcharacternewlineor \n) • Locatesthe cursor afterthenewline • Example: • a.write("hola\n") • Writes in the disk file: • hola\n • ^ (file cursor)

  4. a.close() • “closes” the file • Writes an end of file mark (eof) • Frees resources (memory) associated to the object a • File object cannot be used again, unless another open instruction is performed • example: • a.write(“chao\n”) • a.close(); • hola\nchao\n█ • █ : end of file mark

  5. a=open("lines.txt","r") • a: objectrepresenting file in memory • “opens” (prepares, initializes) filrforreading (“r”) • If file doesnotexists, aborts • Ifexists, locates file cursor at thebeginning of the file • a.close() • “closes” the file • No problema ifomittd (contrary to the case whenwriting)

  6. line=a.readline() • Reads a line from file • Returns a string with the characters of the line (with newline) • Advances cursor at the beginning of next line • Example, if file contains: hola\nchao\n█ • First read returns “hola\n” • Second read returns “chao\n” • Third read detects a █ and returns “” • Note. Because the string has a \n, the program shows: • hola • chao • ¿Solution? print linea[0:len(linea)-1] o linea[0:-1] • Avoids showing last character

  7. Solution 2. show file content • #read and show linesfrom file a=open("lines.txt","r") • #readalllines • for line in a: • #show stringwithoutlastcharacter • print line[0:-1] • #close file • a.close() • ¿for linea in a:? • Readsalllinesfromthe file • In eachiteration a line from file isread and assigned to the variable line

  8. Problem. Copy one fil to another getting both namefiles from user Ejemplo: input?lines.txt output?lines1.txt #open input and output files a=open(raw_input("input?"),"r") b=open(raw_input("output?"),"w") #copy all lines from file for linea in a: b.write(linea) #close files a.close() b.close()

  9. #show lines containing a string #get strings and open file s=raw_input("string to search?") a=open(raw_input(“file?"),"r") #read all lines from file for line in a: #show lines containing string if s in line: print line[0:-1] #close file a.close() Note. s in lineis equivalent to linea.find(s)>=0

  10. #count lines and characters of a file nl=0 #counter for lines nc=0 #counter for characters a=open(raw_input(“file?"),"r") while True: line=a.readline() if line=="": break; nl=nl+1 nc=nc+len(line) a.close() print nl,"lines" print nc,"characteres"

  11. # count lines and characters of a file nl=0 #counter for lines nc=0 #counter for characters a=open(raw_input(“file?"),"r") for line in a: nl=nl+1 for c in line: nc=nc+1 a.close() print nl,"lines" print nc,"characteres"

  12. # count lines and characters of a file nl=0 #counter for lines nc=0 #counter for characters a=open(raw_input(“file?"),"r") for line in a: nl += 1 #nl=nl+1 nc += len(line) #nc=nc+len(line) a.close() print nl,"lines" print nc,"characteres" syntax: variable operator= expression Semantic: variable = variable operator (expression)

  13. #count lines and characters of a file a=open(raw_input(“file?"),"r") s = a.read() a.close() print s.count(“\n”),"lines" print len(s),"characteres“ ¿a.read()? • reads and returns all characters of a file

More Related