So I am on that chapter and another post showed me how to reduce the code to a much more condensed version
from sys import argv
from os.path import exists
script, from_file, to_file = argv
(open(to_file, 'w').write(open(from_file).read()))
My question on line 6 is why cant I use the same format that open(to_file,'w') uses, for the part that says: open(from_file).read()
Can I use something similar like open(from_file, 'r') in that part of the code? Why or why not?
open(something, 'r')andopen(something, "w")? Your problem description is very unclear.open(to_file, 'w').write(open(from_file, 'r').read())Note, I did include'r'in the file being read from.read()becauseopen(filename)returns a file handle object, not a string. In order to get the contents of a file as a string, you must callread()on thefileobject.