Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Matt's newb question 1
I'm working through a book called Learn Python The Hard Way. I'm on an exercise (15) where the author asks you to comment in explanations of what each line of code does. Could you look at my code and explanations and tell me what the right explanations are and if I got any of them right?



# I'm not sure what these do exactly. I think import argv imports the argv library.
from sys import argv
# This line makes it so that this script's activation requires the name of it and another input which is the name of the
# file to be opened
script, filename = argv
# creates a variable called txt which functions as to open the file stated in "file name"
txt = open(filename)
# prints a string that contains the name of the file
print "Here's your file %r:" % filename
# prints out the contents of the file represented by "txt"
# prints a string 
print "Type the filename again:"
# this line creates a variable "file_again" whose contents are supplied by the user. The contents are the txt file name.
file_again = raw_input("> ")
# creates a variable "txt_again" whose contents were supplied by the input the user made in "file_again"
txt_again = open(file_again)
# prints the "file_again" that was opened in the variable txt_again
1. sys is a module that contains "system functionality". sys.argv is a list containing your script's command line arguments. One way to use it would be to write import sys and then sys.argv to access it.

from module import names is an alternative way to import a module that allows you to access the given names without naming the module. That is writing from sys import argv allows you to just write argv whereas import sys would require you to write sys.argv instead.


3-9 are all correct.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Simple newb string question Involute 2 355 Sep-08-2019, 12:50 AM
Last Post: Involute
  please help this newb install pygame iofhua 7 1,153 May-15-2019, 01:09 PM
Last Post: buran
  Newb question: Debugging + Linting Python in Visual Studio Code Drone4four 1 441 Apr-15-2019, 06:19 AM
Last Post: perfringo
  Newb question about %02d %04d bennylava 30 4,491 Mar-05-2019, 11:23 PM
Last Post: snippsat
  Pthyon 3 question (newb) bennylava 11 1,130 Feb-28-2019, 06:04 PM
Last Post: buran
  newb selfie PatM 5 623 Feb-19-2019, 12:20 AM
Last Post: snippsat
  Complete NEWB and openpyxl project Netopia 44 4,057 Jan-18-2019, 08:15 PM
Last Post: Netopia
  Newb Question - Threading in Crons vvarrior 2 751 Jul-20-2018, 08:12 PM
Last Post: vvarrior
  Newb: Simple Explicit Formula Duplicitous 1 1,258 May-05-2017, 07:03 PM
Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)