Created
January 4, 2013 05:05
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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