I thought I was a developer for 2 seconds.
I got a little too excited and though I would do my own version of exercise 17 and combine it with some user input.
from sys import argv
script, from_file, to_file = argv
#Open from_file with writeable permission and assigning it to a variable
fromfile = open(from_file,'w')
data_input = input("We need some data to put into the file >>>> ")
#write new input text into the from_file
fromfile.write(f"""{data_input}
THATS IT""")
#load new data from open from_file into a new variabel
from_data = fromfile.read()
print(f"""The new file is {len(from_data)} characters long.
hit RETURN to continue.""")
new_file = open(to_file, "w")
new_file.write(fromfile)
new_file.close()
fromf.close()
But get an error on line 10
from_data = fromfile.read()
io.UnsupportedOperation: not readable
I donât get why I shouldnât be able to read the text fromfile into a new variable?
The first thing that came to my mind was that you have three pair of â around the text inside the brackets.
Try with just one on each side.
fromfile.write(f"{data_input} THATS IT")
But I am also a little bit curious if the f-string can be used like this?
I have not seen it before. Or tried it yet.
Can be useful if it works I guess.
@ktrager - in your argv arguments you declare âfrom_fileâ (with an underscore) but the rest of your file uses âfromfileâ (no underscore).
I suspect once that bug is fixed youâll get a complaint about the double use of ââ as @ulfen69 has highlighted. (This looks like IDE auto-complete which can be a PITA sometimes).
So many hours spent on bugs like theseâŚdonât be discouraged from going off-piste though. You discover some crazy stuff that helps you grow.
I tried to change my names for the various variables and take the the away the ââ"
I still get this in the terminal:
Traceback (most recent call last):
File âex17_extra.pyâ, line 13, in
newread = data_fromfile.read()
io.UnsupportedOperation: not readable
Iâm not sure why this wouldnât work as Im reading whats inside the variable data_fromfile?
Or is it me who completely lost?
my code now like this:
from sys import argv
script, from_file, to_file = argv
#Open from_file with writable permission and assing it to a variable fromfile
data_fromfile = open(from_file,'w')
data_input = input("We need some data to put into the file >>>> ")
#write new input text into the from_file
data_fromfile.write(f"{data_input} THATS IT")
#load new data from open from_file into a new variabel
newread = data_fromfile.read()
print(f"The new file is {len(newread)} characters long. hit RETURN to continue.")
new_file = open(to_file, "w")
new_file.write(newread)
new_file.close()
data_fromfile.close()
I found something that caused the problem I think.
After the input variable is written to the file it is closed.
So I added a new line that opened the file again. Then I worked for me.
I hope it make your code work too.
from sys import argv script, from_file, to_file = argv
#Open from_file with writable permission and assing it to a variable fromfile
data_fromfile = open(from_file,'w')
data_input = input("We need some data to put into the file >>>> ")
#write new input text into the from_file
data_fromfile.write(f"{data_input} THATS IT")
#load new data from open from_file into a new variabel
data_fromfile = open(from_file, 'w') # **I added this line with 'w' as writeable**
newread = data_fromfile.read()
print(f"The new file is {len(newread)} characters long.
hit RETURN to continue.")
new_file = open(to_file, "w")
new_file.write(newread)
new_file.close()
data_fromfile.close()
Iâm not sure how you get it to work?
I added the extra open() as you suggested, but still receive the same error message.
Then I copy/pasted your code just to make sure itâs not just me who misspelled something but I also get the same error.
This is my full terminal
$ python3.6 ex17_extra.py filefrom.txt fileto.txt
We need some data to put into the file >>>> some sample text!!!
Traceback (most recent call last):
File âex17_extra.pyâ, line 13, in
newread = data_fromfile.read()
io.UnsupportedOperation: not readable
Thank you @zedshaw for the straight forward explanation.
I thought once the file is ope() n then it stays open until a close()?
Does the âDVDâ automatically âejectâ after I write to it?
I tried to open the file again as @ulfen69 also suggested, but still getting the same error.
It stays open, but if you read to the end, then itâs kind of like you ran the DVD to the end and are now trying to play it more. Think for second about it being like a DVD (or really a VHS tape). If you are at the end of the DVD and then try to force it to read again from the end what would the DVD (VHS tape) player do? Itâd give you some kind of error or refuse to play.
What youâre doing is running the DVD/VHS to the end, then expecting it to keep readingâŚfrom the end? You have to rewind it, which is what .seek() does. Read through those docs I dropped you to see what I mean. Search for seek(). If you just rewind it, then itâll work as you expect.
Iâm running a seek(0) to take the âreaderâ back to the start of the document.
then I try to read() the file, but i still run into trouble.
$ python3.6 ex17_extra.py test1.txt test2.txt
We need some data to put into the file >>>> Test Test Test
Traceback (most recent call last):
File âex17_extra.pyâ, line 13, in
newread = data_fromfile.read()
io.UnsupportedOperation: not readable
from sys import argv
script, from_file, to_file = argv
#Open from_file with writable permission and assing it to a variable fromfile
data_fromfile = open(from_file,'w')
data_input = input("We need some data to put into the file >>>> ")
#write new input text into the from_file
data_fromfile.write(f"{data_input} THATS IT")
#rewind data file and load text into newread variable
data_fromfile.seek(0)
newread = data_fromfile.read()
print(f"The new file is {len(newread)} characters long. hit RETURN to continue.")
new_file = open(to_file, "w")
new_file.write(newread)
new_file.close()
data_fromfile.close()
Hi @ktrager - itâs not as simple as that you are specifying the first open file with âwâ for writeable state, rather than readable ârâ.? I know âwâ should truncate if the file exists, but try opening for reading. Then writing as a separate action to see if it has an impact.
And, itâs not being thick! This sort of debugging is part of the job.
I got itâŚ
Basically I thought the âwâ is the same as âr+â. I changed that and it works.
Thank you.
I intellectually know that no question are stupid question when learning - however sometime I feel I should know. @zedshaw VHS analogy with the âpermission stateâ did the trick.
Nah, youâd have to be an old fart like me a literally grow up using actual casette tapes in a old computer when youâre a kid to know this âVHS tapeâ stuff. You shouldnât hold yourself to that.
That reminded me when I and a friend tried to copy (ilegally) a TV game.
We put the tape in his parents stereo. Then we put a small tape recorder at one of the speaker. Turned on the stereo with the volyme at maximum level.
We had to go to the kitchen while recording.
Of course it didnât work
My first experiment in the software trade.
But it was fun trying.
It was about 42 years ago.