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?
>>> 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.
>>>
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$
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
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 //
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 :)
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,
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!
ex 13 did anyone done this
Combine raw_input with argv to make a script that gets more input from a user.
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$
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
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
Thank you it worked
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 //
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 :)
Thank you so much.
Good video course ware by MIT
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6...