Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matt's newb question 1
#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?

Thanks,

Matt

# 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"
print txt.read()
# 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
print txt_again.read()
Reply
#2
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.


2.https://stackoverflow.com/questions/13666346/from-sys-import-argv-what-is-the-function-of-script

3-9 are all correct.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError issue with daughter's newb code MrGonk 2 1,406 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  Simple newb string question Involute 2 2,169 Sep-08-2019, 12:50 AM
Last Post: Involute
  please help this newb install pygame iofhua 7 5,849 May-15-2019, 01:09 PM
Last Post: buran
  Newb question: Debugging + Linting Python in Visual Studio Code Drone4four 1 2,390 Apr-15-2019, 06:19 AM
Last Post: perfringo
  Newb question about %02d %04d bennylava 30 19,153 Mar-05-2019, 11:23 PM
Last Post: snippsat
  Pthyon 3 question (newb) bennylava 11 5,743 Feb-28-2019, 06:04 PM
Last Post: buran
  newb selfie PatM 5 3,545 Feb-19-2019, 12:20 AM
Last Post: snippsat
  Complete NEWB and openpyxl project Netopia 44 16,885 Jan-18-2019, 08:15 PM
Last Post: Netopia
  Newb Question - Threading in Crons vvarrior 2 2,721 Jul-20-2018, 08:12 PM
Last Post: vvarrior
  Newb: Simple Explicit Formula Duplicitous 1 3,108 May-05-2017, 07:03 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020