"Learn Python the Hard Way" exercise 17 pages 56 to 58

I typed my ex17.py (page 56) into my text editor (Notepad ++) on my Windows 7 laptop. I have checked my code twice for errors, and I can find none.

On PowerShell, when I type:

python ex17.py test1.txt new_file.txt

I see the following on the PowerShell screen:

Copying from test1.txt to new_file.txt
Traceback (most recent call last):
File “ex17.py”, line 12, in (module)
print(f"The input file is {len(indata)} bytes long")
TypeError: object of type ‘builtin_function_or method’ has no len()

I did not use the “echo” or “cat” functions in my PowerShell like you did on page 57 because I really do not understand how to do that on Windows 7. Is it necessary that I use those 2 functions in PowerShell to make this exercise 17 work properly? I did read and work the examples in the appendix “Command Line Crash Course”, but unfortunately I cannot see how to use the “echo” and “cat” functions.

I tried creating a “new_file.txt” in my Notepad ++ text editor with only one short string in it (since new_file.txt did not previously exist), but that resulted in the same error message.

Also, test1.txt does definitely exist. I created it in the previous exercise (i.e., exercise 16).

Any help would be greatly appreciated.

Hi, can you post the whole part of the script from lines 1 to 12 included? Make sure you add the ticks before and after so it is well formatted (this text is in back ticks) `

Sure. Here is the script (ex17.py) copied directly from my text editor (Notepad ++):

from sys import argv
from os.path import exists

script, from_file, to_file = argv

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

# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read

print(f"The input file is {len(indata)} bytes long")

print(f"Does the output file exist? {exists(to_file)}")
print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()

out_file = open(to_file, 'w')
out_file.write(indata)

print("Alright, all done.")

out_file.close()
in_file.close()

I have checked this script multiple times with the script (ex17.py) on page 56 of “Learn Python 3 the Hard Way”, and I can find no differences.

…check line 10:
It should be:
indata = in_file.read()

That worked! Thank you so very much for your help! I apologize for making such a careless and stupid mistake. I honestly did check it at least 3 times, and I am amazed that I did not see the lack of the parentheses after the read function. I will not make that kind of mistake again. Thanks again.

Hey, no problem. And trust me, you will make this kind of mistake again countless times :-))
We are not robots.

Thanks for helping @io_io, and yes, the top #1 primary error everyone makes is 1-2 character typos. It’s very annoying.

1 Like