Skip to content

Instantly share code, notes, and snippets.

@Funk3
Last active October 15, 2017 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Funk3/85cd66eceb0b630c4fea52e3a2de5c81 to your computer and use it in GitHub Desktop.
Save Funk3/85cd66eceb0b630c4fea52e3a2de5c81 to your computer and use it in GitHub Desktop.
Code shortening.
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()
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)
@Funk3
Copy link
Author

Funk3 commented Oct 15, 2017

wut. I mean in that line it's a string. but in the argv argument, you are typing in the name of a file

@mattj1
Copy link

mattj1 commented Oct 15, 2017

Yes, but I'm assuming you're trying to print the number of bytes in the file, not the number of characters in the file name

@Funk3
Copy link
Author

Funk3 commented Oct 15, 2017

oh, I understand what you mean now.

Thats what len does. I was just shown the len function in this exercise, and wrongly assumed that it was bytes of the file... because isn't that as number of characters when it comes to a text file?

@mattj1
Copy link

mattj1 commented Oct 15, 2017

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

@Funk3
Copy link
Author

Funk3 commented Oct 15, 2017

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.

@mattj1
Copy link

mattj1 commented Oct 15, 2017

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)

@Funk3
Copy link
Author

Funk3 commented Oct 15, 2017

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