Skip to content

Instantly share code, notes, and snippets.

@santosadrian
Created February 5, 2020 05:51
Show Gist options
  • Save santosadrian/743b3fd7f573be6d72d9d6f7c6e27004 to your computer and use it in GitHub Desktop.
Save santosadrian/743b3fd7f573be6d72d9d6f7c6e27004 to your computer and use it in GitHub Desktop.
Example 20.1
from sys import argv # from module sys import object named argv
script, input_file = argv # set the variables from command line
def print_all(f): # defines variable that is a file and a function object ???
print(f.read()) # what do the function, prints all that is written in (f)ile
def rewind(f: object) -> object: # defines function
f.seek(0) # rewind to the start of f (input_file)
def print_a_line(line_count, f): # defines another function with two variables
print(line_count, f.readline()) # read the line where is positioned python and prints it in the screen
current_file = open(input_file) # set the variable current_file to object open, and opens the file that I put in command line running the script
print("First let's print the whole file:\n") # prints and add empty line after
print_all(current_file) # calls, run or use the function print_all with prints the variable current_file that has open(input_file)
print("Now let's rewind, kind of like a tape.")
rewind(current_file)
print("Let's print three lines:") # prints out first three lines after writing 1, 2 and 3 into each line.
current_line = 1
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_line, current_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment