Holy poopnoodle. Question about variables ex17

Alright. So this one was a doozy, and I took Zeds personal challenge to bring those script as short as humanly possible.

If you take a look there (I will bring my code into here so you can see),

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print(f"This file is {len(from_file)} bytes long")
print(f"Does the output file exist? {exists(to_file)}")
input(“Press enter to write file.”)

f=open(from_file).read()
open(to_file, ‘w’).write(f)

having the variable is ABSOLUTELY essential for me to be able to run the write command and have it write whats inside the file as opposed to the filename? like the script runs the command in the variable then moves on to the next line which opens the to_file to actually write whats inside from_file?

When I initially did this, I would go into powershell and go

more new_file.txt

and all it would say is

test.txt

so I tried fixing this for about 30 minutes until I created the open variable.

I hope that I understood it properly before I move on.

Nevermind. It just clicked for me.

It doesn’t run the f variable.

It runs the variable during the last line when .write is interpreted.

Holy poopsicle. I COMPREHEND!

3 Likes