# Assignment 8 exercise 2 # Write to a file and then read from it. # Cory Parker 5/18/15 #This will open the file for writing. myFile = open("names.dat",'w') # This will write a few names to the file myFile.write("Scotty\n") myFile.write("Jethro\n") myFile.write("Gertrude\n") myFile.write("Vergil\n") myFile.write("Geigh\n") myFile.close() myFile = open("names.dat",'r') #Counter will go here when I figure out how to code it with open('names.dat') as myfile: count = sum(1 for line in myfile) print ('Number of names in this file: {}.'.format(count))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question