Advertisement
namchef

LPTHW_ex15

Sep 28th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. from sys import argv
  2.  
  3. script, filename = argv
  4.  
  5. txt = open(filename)
  6.  
  7. print "Here's your file %r:" % filename
  8. print txt.read()
  9. txt.close()
  10. print "Type the filename again:"
  11. file_again = raw_input("> ")
  12.  
  13. txt_again = open(file_again)
  14.  
  15. print txt_again.read()
  16. txt_again.close()
  17.  
  18. ##PYTHON SHELL
  19. ## >>> import sys
  20. ## >>> sys.argv=['','sample.txt']
  21. ## >>> execfile('ex15.py')
  22.  
  23. # extra credit
  24. #    Get rid of the part from line 10-15 where you use raw_input and try the script then.
  25. #    Use only raw_input and try the script that way. Think of why one way of getting the filename would be better than another.
  26. #    Run pydoc file and scroll down until you see the read() command (method/function). See all the other ones you can use? Skip the ones that have __ (two underscores) in front because those are junk. Try some of the other commands.
  27. #    Startup python again and use open from the prompt. Notice how you can open files and run read on them right there?
  28. #    Have your script also do a close() on the txt and txt_again variables. It's important to close files when you are done with them.
  29.  
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement