I went off piste and have been starring at my code and can't figure out what I'm doing wrong

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?

Hi @ktrager

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.

@ulfen69 & @gpkesley

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()

Hello @ktrager

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()
1 Like

I believe your misconception is this:

A file is like a DVD in a DVD player. When you call .close() it’s like ejecting the DVD and putting back in its case. What you did then is this:

  1. Play DVD, it gets to the end.
  2. Eject the DVD and put it away.
  3. Run back to the player and hit play. It fails, because you ejected it and put it away.

What you have to do it put the DVD back in the player by opening the file again.

The other thing to read about is seek, which will let you write to and read until the end, then rewind or fastforward the file:

https://docs.python.org/3/tutorial/inputoutput.html

1 Like

Thanks for this @ulfen69

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.

Opps…
I think I did some mistake when copy and paste into the post.

On first line.

script, from_file, to_file = argv

Must be on next line.

data_fromfile = open(from_file, ‘r’)

Should have been ‘r’ because it it is supposed to be read or as in this case measured of its lenght

and the following print statement has been “returned”
It should be on one line.

Sorry for this…

1 Like

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.

2 Likes

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()

Sorry for being so thick.

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.

1 Like

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.

Thank you.

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.

I think I might just be a late code ‘bloomer’ :wink:
My favorite VHS when I was a kid was a home-recorded flipper film.

Betamax was far superior :wink:

Glad you sorted it.

:upside_down_face:
”casette tape”.

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.

2 Likes

That’s exactly how my sister and I recorded the Top 40 off the radio years ago. Ah, the good old days!

1 Like