# Josh Roybal # CIS 122 #Chapter 10 Programming Exercise #2 import os import os.path from random import randint fileName = "./names.dat" name1 = "Josh" name2 = "Sarah" name3 = "Jon" name4 = "Shea" name5 = "Jeanne" if os.path.isfile(fileName) and os.access(fileName, os.R_OK): print("File exists and is readable") with open(fileName, 'w') as file: print("File is open for writing! \n") file.write(name1) file.write("\n") file.write(name2) file.write("\n") file.write(name3) file.write("\n") file.write(name4) file.write("\n") file.write(name5) else: print("File is missing or unreadable") with open(fileName, 'w') as file: print("Creating file... \n") file.write(name1) file.write("\n") file.write(name2) file.write("\n") file.write(name3) file.write("\n") file.write(name4) file.write("\n") file.write(name5) with open(fileName, 'r') as file: count = 0 while count < 5: print(file.readline()) count += 1
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