Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Created January 4, 2013 05:05
Show Gist options
  • Save gogsbread/4450104 to your computer and use it in GitHub Desktop.
Save gogsbread/4450104 to your computer and use it in GitHub Desktop.
IO operations in python. Read and sum values in a file. Good practice problem for learning basic IO operations in python
def Sum(filePath):
total = 0
with open(filePath,'rU') as f:
line = f.readline()
while(line != ''):
value = int(line)
total+=value
line = f.readline()
return total
if __name__ == '__main__':
print Sum('temp.txt')
''' Sample temp.txt
temp.txt
*********
1
2
32
''''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment