This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Learn Python the Hard Way

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Week 4: Ex 13-16, Due Feb 20

Go back to: General discussion
Exercise 13: Parameters, Unpacking, Variables 35
 
Exercise 14: Prompting And Passing 37
 
Exercise 15: Reading Files 39
 
Exercise 16: Reading And Writing Files 41
 
 
Red Olaf's picture
Red Olaf
Wed, 2011-02-16 01:24

Hello, I have a question about exercise 15 extra credit number 7:

Startup python again and use open from the prompt. Notice how you can open files and run read on them right there?

I can start up python in the terminal, but I can't get the "open" command to work from the prompt. At best I get a NameError. Can someone put up an example of how they opened from the python prompt?

Thanks,

Red Olaf's picture
Red Olaf
Wed, 2011-02-16 01:25

Ha. Got it:

>>> f = open("ex15_sample.txt")
>>> print f.read()
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
>>>

This is so much fun!

nagarjuna c's picture
nagarjuna c
Thu, 2011-02-17 13:51

ex 13 did anyone done this
Combine raw_input with argv to make a script that gets more input from a user.

Red Olaf's picture
Red Olaf
Thu, 2011-02-17 20:56

Hello, this is what I came up with. It works, but I'm not sure if this is exactly what was asked for:

name = raw_input("What is your name? ")
age = raw_input("How old are you? ")
weight = raw_input("How much do you weigh? ")
height = raw_input("How tall are you? ")

from sys import argv

script, first, second, third, fourth = argv

print "This is your name:", name
print "This is you age:", age
print "This is your weight:", weight
print "This is your height:", height

Output:

b98-015-libsta-134-71-59-34:~/python tqhe$ python ex13ec4.py name age weight height
What is your name? Max
How old are you? 35
How much do you weigh? 150
How tall are you? 6'10"
This is your name: Max
This is you age: 35
This is your weight: 150
This is your height: 6'10"
b98-015-libsta-134-71-59-34:~/python tqhe$

nagarjuna c's picture
nagarjuna c
Fri, 2011-02-18 12:41

I tried same but did not work for me :(

name = raw_input("What is your name? ")
age = raw_input("How old are you? ")
weight = raw_input("How much do you weigh? ")
height = raw_input("How tall are you? ")

from sys import argv

script,first,second,third,fourth = argv

print "This is your name:", name
print "This is you age:", age
print "This is your weight:", weight
print "This is your height:", height

error

C:\lphw>python ex_c16.py
What is your name? p2pu
How old are you? 12
How much do you weigh? 234
How tall are you? 176
Traceback (most recent call last):
File "ex_c16.py", line 8, in
script,first,second,third,fourth = argv
ValueError: need more than 1 value to unpack

Red Olaf's picture
Red Olaf
Fri, 2011-02-18 21:25

Hello, you need to add the variable names when you run the script in the terminal:

$ python ex13ec4.py name age weight height

Read page 36 of LPTHW:

Run the program like this:

python ex13.py first 2nd 3rd

That should help you.

HTH

nagarjuna c's picture
nagarjuna c
Mon, 2011-02-21 13:26

Thank you it worked

nagarjuna c's picture
nagarjuna c
Thu, 2011-02-17 14:20

i tried exercise 15 little different way but got erros please help
from sys import argv

script, filename = argv
# open the file
txt = open(filename)
# prints the file name
print " Here is your file %r:" % filename
# prints the content of the file
print txt.read()
# I am trying to open the same file
f = open(filename)
#inserting new line
lostline = raw_input('>')
# writing new line to the file
f.write(lostline)
# closing the the file
f.close()
print f.read()

## giving me error
C:\lphw>python ex15_e.py nagarjuna
Here is your file 'nagarjuna':
naga line1
naga line 2
naga line 3

>lost line
Traceback (most recent call last):
File "ex15_e.py", line 15, in
f.write(lostline)
IOError: [Errno 0] Error //

Red Olaf's picture
Red Olaf
Thu, 2011-02-17 21:45

Hello, I played around with your code a bit. From what I see, you are trying to read a text file, then add a new line to the text file. This is my code and it works, but it erases what was in the text file originally so be careful:

from sys import argv

script, filename = argv
# open the file
txt = open(filename)
# prints the file name
print "Here is your file %r:" % filename
# prints the content of the file
print txt.read()
#inserting new line
print "Please add a new line:"
lostline = raw_input('> ')
# writing new line to the file
output = open(filename, 'w')
output.write(lostline)
#prints the new content
print "Here is the new line:"
output = open(filename)
print output.read()
# closing the the file
output.close()

In terminal:

b98-015-libsta-134-71-59-34:~/python tqhe$ python user.py user.txt
Here is your file 'user.txt':
This is the old text
in my test file that
will be replaced by the
new line.
Please add a new line:
> This is the new text in the file.
Here is the new line:
This is the new text in the file.
b98-015-libsta-134-71-59-34:~/python tqhe$

Hope this helps. I'm learning, too. so this could be way off :)

nagarjuna c's picture
nagarjuna c
Fri, 2011-02-18 07:32

Thank you so much.