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!