Plutonic Rainbows

Limiting File Size to Copy with Python

This script uses some of the things that I have learnt so far; open(), read() and len(). I use import argv to pass in the file I wish to use.

The script then makes a decision using if and else as to copying it based on the file size. In this case, 290418 bytes. Once copied, the files are then closed, as this is good practice in Python.

from sys import argv

script, file_from, file_to = argv

file_in = open(file_from)
indata = file_in.read()

print "\nThe file size is %s bytes.\n" % len(indata)

if len(indata) > 290418:
	print "Sorry. The file is too large to copy."
	
	file_in.close()

else:
	raw_input("\nPress enter to copy file.\n")

	file_out = open(file_to, 'w')
	file_out.write(indata)

	print "All done."

	raw_input("Press enter to close the files.\n")

	file_in.close()
	file_out.close()