170 likes | 181 Views
This text provides an overview of file operations in disk, including opening, writing, reading, and closing files, along with examples and syntax.
E N D
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
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)
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
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)
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
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
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()
#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
#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"
# 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"
# 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)
#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