Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Merge #18

Merged
merged 5 commits into from
Jun 24, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions projects/Random_word_from_list/Random_word_from_list.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import sys
import random

# check if filename is supplied as a command line argument
if sys.argv[1:]:
filename = sys.argv[1] # check if filename is supplied as a command line argument
filename = sys.argv[1]
else:
filename = input("What is the name of the file? (extension included): ")

try:
file = open(filename)
except:
print("File doesn't exist!") # handle exception
except (FileNotFoundError, IOError):
print("File doesn't exist!")
exit()
# handle exception

num_lines = sum(1 for line in file if line.rstrip()) # get number of lines
# get number of lines
num_lines = sum(1 for line in file if line.rstrip())

random_line = random.randint(0, num_lines) # generate a random number between possible interval
# generate a random number between possible interval
random_line = random.randint(0, num_lines)

file.seek(0) # re-iterate from first line
# re-iterate from first line
file.seek(0)

for i, line in enumerate(file):
if i == random_line:
print(line.rstrip()) # rstrip removes any trailing newlines :)
print(line.rstrip()) # rstrip removes any trailing newlines :)
break