Download presentation
Presentation is loading. Please wait.
Published byLuke Grant Modified over 9 years ago
1
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas
2
Files File is also one of built-in type in Python The built-in OPEN function creates a Python file object, which servers as a link to a file After calling open, you can read and write the associated external file
3
Common FILE operations Input=open(‘file_name.txt’, ‘r’) Output=open(‘file_name.txt’, ‘w’) Input.readlines() Input.close(); Output.close()
4
Split() function >>> string= ‘a b c d e’ >>> string.split() ['a', 'b', 'c', 'd', 'e']
5
File in Action Let’s try to read in a file!!
6
File in Action input=open('file.txt','r') all=[] for line in input.readlines(): temp=line.split() all.append(temp)
7
File in Action Then we can calculate the average score for each student
8
File in Action for i in range(len(all)): sum=0.0 for j in range(1,len(all[i])): sum=sum+int(all[i][j]) average=sum/5 print average
9
Python Official Sites Python Tutorial http://docs.python.org/tut/ Python Official Site http://python.org/ Download Python Latest Version http://python.org/download/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.