Last active
October 15, 2017 04:27
-
-
Save Funk3/85cd66eceb0b630c4fea52e3a2de5c81 to your computer and use it in GitHub Desktop.
Code shortening.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import argv | |
from os.path import exists | |
script, from_file, to_file = argv | |
print(f"Copying from {from_file} to {to_file}") | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | |
#you need the variable in order to use the write command | |
#you cannot run commands inside the write function | |
#open it in a variable first then you can write it. | |
f = open(from_file).read() | |
open(to_file, 'w').write(f) |
oh....... thats why in line 8 the author had him open the file first then use the len function.
Then it was closed afterwards. Thats a good catch.
also last line should be open(to_file, 'w').write(f)
not from_file (you're just writing the old file with its own contents)
haha yeah, I didn't revise it with my latest script. Good catch again.
it just clicked though. it has to be a string in the write() function, so I had to create a variable that would open the file so it can be read from to write into the to_file.
It was a bit weird... but then it clicked.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from_file is only a string, from what I can tell, since it comes from argv. You'd have to open and read the file (which you do later) or use some other method to find the size.
https://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python