# Josh Roybal # CIS 122 # Chapter 10 Programming Exercise #1 import os import os.path from random import randint fileName = "./numbers.dat" count = 0 SIZE = 10 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") while count < SIZE: num = str(randint(0, 1000)) file.write(num) file.write("\n") count += 1 else: print("File is missing or unreadable") with open(fileName, 'w') as file: print("Creating file... \n") while count < SIZE: num = str(randint(0, 1000)) file.write(num) file.write("\n") count += 1 with open(fileName, 'r') as file: count = 0 while count < SIZE: 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