Skip to content

Instantly share code, notes, and snippets.

@macloo
Last active January 3, 2016 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macloo/8493442 to your computer and use it in GitHub Desktop.
Save macloo/8493442 to your computer and use it in GitHub Desktop.
These tiny, simple files are derived from Zed Shaw's free online book _Learn Python the Hard Way_. These files accompany Zed's exercises 15 - 17. I wrote a quiz for students to test themselves - see the URL at the top of each file here. There are 4 Python files and 2 text files.
# see http://bit.ly/mmpython2 for the quiz
# compare the way this runs to the way argv_test2.py runs
# to run:
# python argv_test1.py first.txt
from sys import argv
script, filename = argv
txt = open(filename)
print txt.read()
print "-" * 10
print filename
txt.close()
# see http://bit.ly/mmpython2 for the quiz
# compare the way this runs to the way argv_test1.py runs
# the filename you type must be the name of a file in the same folder with this file
# to run:
# python argv_test2.py
print "Type the name of a text file." # e.g. gators.txt
filename = raw_input("> ")
txt = open(filename)
print txt.read() # comment this line out and run again. Compare
print "-" * 10
print filename
txt.close()
# see http://bit.ly/mmpython2 for the quiz
# CAREFUL - do not give the name of a file you want to keep
# use a junk file only! Or name a file that does not exist
# after you run this, check the folder conatining this file & check the contents of the junk file
# to run:
# python argv_test3.py junkfile.txt
from sys import argv
script, filename = argv
target = open(filename, 'w')
target.close()
# see http://bit.ly/mmpython2 for the quiz
# to run:
# python argv_test4.py gators.txt first.txt
from sys import argv
script, file1, file2 = argv
f1 = open(file1)
f2 = open(file2)
print '''
The length of %s is %d bytes, and the length of %s is %d bytes.
''' % (file1, len(f1.read()), file2, len(f2.read()))
f1.close()
f2.close()
Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the Government for a redress of grievances.
'Neath the Orange and Blue victorious
Our love shall never fail.
There's no other name so glorious—
All hail, Florida, hail!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment