Ex17 - with a little extra printing


#1

Hi,
Working on ex17 and i have it running as per the lesson but i wanted to try something a bit extra at the end.

Basically after copying “from_file” to “to_file” i wanted to print the contents of “to_file”
I also added a line to print the contents of “from_file” at the start which works fine, but for some reason i’m unable to print the contents of “to_file” at the end. Here is my code:

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print(f"Copying from {from_file} to {to_file}")

#creates a variable called "in_file" from the argv "from_file", opens it and reads it.
in_file = open(from_file).read()
print(f"The contents of 'from_file' is:\n {in_file}")

print(f"The input file is {len(in_file)} bytes long")
print(f"Does the output file exist? {exists(to_file)}")
print(f"Ready, hit RETURN to contiune, CTRC-C to abort")
input(">")

#creates variable "out_file" and opens the "to file" argv 'w' means writables
out_file = open(to_file, 'w')

#writes data from "in_file to "out_file"
out_file.write(in_file)

print("Alright, all done")


blah = open(to_file).read()
print(f"We printed: {blah.read}")

When i run it i get:

$ python3 ex17.py test.text new_file.text
Copying from test.text to new_file.text
The contents of 'from_file' is:
 This is a test file.

The input file is 21 bytes long
Does the output file exist? True
Ready, hit RETURN to contiune, CTRC-C to abort
>
Alright, all done
Traceback (most recent call last):
  File "ex17.py", line 27, in <module>
    print(f"We printed: {blah.read}")
AttributeError: 'str' object has no attribute 'read'

I know i’m missing something basic here but i can’t seem to make it work. Plz halp!


#2

Hi @rogerc

The reason for your error is inside the “{}” brackets.
You just have to put the variable in there {blah}.


#3

Hi! that solved the error, but it doesn’t actually print the contents of the new file.

$ python3 ex17.py test.text new_file.text     
Copying from test.text to new_file.text
The contents of 'from_file' is:
 This is a test file.

The input file is 21 bytes long
Does the output file exist? False
Ready, hit RETURN to contiune, CTRC-C to abort
>
Alright, all done
We printed:

#4

Hello again.
I can see a: input(">").

Do you want to write something into the file?
Use a variable to connect the input function to.

Example:

user_input = input('write something please. :> ' )

(raw_input for Python 2.7)


#5

That doesn’t have anything to do with the problem i’m seeing where the file ins not printing.
the input() is part of the exercise


#6

Oh i fixed it. I need to close the file before reading it. So i added out_file.close() before opening it again and that solved it

$ python3 ex17.py test.text new_file.text
Copying from test.text to new_file.text
The contents of 'from_file' is:
 This is a test file.

The input file is 21 bytes long
Does the output file exist? True
Ready, hit RETURN to contiune, CTRC-C to abort
>
Alright, all done
 We printed:
This is a test file.

#7

Ah, yes, close it then re-open it. The other option is to use seek(0) to rewind to the beginning.