1 / 34

File I/O

File I/O. Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012. File. Files: named storage compartment on your computer that are managed by operating system The built-in “open” function creates a Python file object, which serves as a link to a file in your computer.

Download Presentation

File I/O

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. File I/O Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012

  2. File • Files: named storage compartment on your computer that are managed by operating system • The built-in “open” function creates a Python file object, which serves as a link to a file in your computer

  3. Open Function • After calling open, you can read or write the associated external file • Two major types of open file function: • Read file (‘r’) • Write file (‘w’)

  4. Read/Write file • Open function take two variables, first on is the file name you want to deal with, another one is read or write of the file • input = open ('file1.txt','r') Variable name Keyword file name read file • output = open (‘output_file.txt’, ‘w’) Variable name Keyword file name write file

  5. Read/Write file • In the end of the program, remember to close the file via close() function • Input.close() • Output.close()

  6. Read in File1

  7. Read File • After connect with the file through open function, you need “readlines()” to read the contents in the file • readlines() is the function that read entire file into list of line strings

  8. Read File input = open ('file1.txt','r') input.readlines() • But this is not good enough, because the contents we read is in text format, we need to convert it into numbers and store those in a list, so that we can do some computations on those numbers

  9. Read File1

  10. Read File • After we read in those files into list ‘all’, we can easily calculate average, max, min…

  11. Class Practice • Download file1 into the Python folder • Read in the file and print out the max value

  12. Write file • We have a “write” function for write things on a file • Things you want to write MUST be string, so if you want to write a number, you have to convert it by str() output = open (‘output_file.txt’, ‘w’) output.write(“Hello World!!!”) output.write(str(12))

  13. Write file • write number form 0 to 10 into a file output = open (‘output_file.txt’, ‘w’) for i in range(11): output.write(str(i)) output.write(‘\n’) output.close()

  14. Class Practice • Write the following half pyramid into a new created file named “pyramid.txt” * ** *** **** *****

  15. Read File • Now, think about different file type we have…

  16. Read in File2

  17. Read in File2 • Since all numbers are in one line, we cannot read this file like we did in file1 • We need to “separate” the numbers through space

  18. Read in File2 • As the result, we need the function “split(str)” • This method returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified)

  19. split() examples >>> sentence = "the cat sat on the mat" >>> print sentence.split() ['the', 'cat', 'sat', 'on', 'the', 'mat'] >>> sentence = "the,cat,sat,on,the,mat“ >>> print sentence.split(',') ['the', 'cat', 'sat', 'on', 'the', 'mat']

  20. Class Practice >>> sentence = "the cat sat on the mat" >>> print sentence.split(‘t’)

  21. Read in File2 input=open('file2.txt','r') for line in input.readlines(): all=line.split() for i in range(len(all)): all[i]=int(all[i])

  22. Read in File3

  23. 2D list: lists inside of list • Here’s the way to create a 2D list (Just like what we saw last week) aa=[1,2,3] bb=[4,5,6] cc=[7,8,9] matrix=[] matrix.append(aa) matrix.append(bb) matrix.append(cc)

  24. Access 2D list • With one index, you get an entire row: >>> matrix [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> matrix[0] [1, 2, 3] >>> matrix[1] [4, 5, 6] >>> matrix[2] [7, 8, 9]

  25. Access 2D list • With two index, you get a item within the row: >>> matrix[0][0] 1 >>> matrix[0][1] 2 >>> matrix[1][1] 5 >>> matrix[1][2] 6 >>> matrix[2][2] 9

  26. Len() function on Matrix • len() function can tell the size of the list By the same token: >>>len(matrix) 3 >>>len(matrix[0]) 3

  27. Matrix • If we have another matrix looks like: >>> matrix [[10, 20, 30, 40, 50, 60, 70, 80], [11, 12, 13, 14, 15, 15], [15, 25, 35, 45, 55, 65, 75, 85, 95]] >>> matrix[0] [10, 20, 30, 40, 50, 60, 70, 80] >>> matrix[1][1] 12 >>> matrix[0][8] error

  28. Matrix >>> len(matrix) 3 >>> len(matrix[0]) 8 >>> len(matrix[1]) 6 >>> len(matrix[2]) 9

  29. Append function • If we have a matrix=[[1,2,3],[4,5,6],[7,8,9]] after >>> matrix.append(3) the matrix looks like: therefore, >>> matrix[3] 3

  30. Append function • If we have a matrix=[[1,2,3],[4,5,6],[7,8,9]] after >>> matrix[1].append(3) the matrix looks like: therefore, >>> matrix[1][3] 3

  31. Append function • If we have a matrix=[[1,2,3],[4,5,6],[7,8,9]] after >>> matrix.append([1,2,3]) the matrix looks like: therefore, >>> matrix[3] [1,2,3]

  32. Read File3 input=open('file3.txt','r') matrix=[] for line in input.readlines(): matrix.append(line.split())

  33. Print out the average score of each student input=open('file3.txt','r') matrix=[] for line in input.readlines(): matrix.append(line.split()) for i in range(len(matrix)): sum=0 for j in range(len(matrix[i])): sum+=int(matrix[i][j]) avg=sum/len(matrix[i]) print avg

  34. Write the average score of each student to file input=open('file3.txt','r') output=open('avg.txt','w') matrix=[] for line in input.readlines(): matrix.append(line.split()) for i in range(len(matrix)): sum=0 for j in range(len(matrix[i])): sum+=int(matrix[i][j]) avg=sum/len(matrix[i]) print avg output.write(str(avg)+'\n') input.close() output.close()

More Related