simultaneous copy to multiple media

Tim Chase python.list at tim.thechases.com
Thu Aug 31 10:39:48 EDT 2006


> I need a program that simultaneously can copy a single file (1
> GB) from my pc to multiple USB-harddrives.

Sounds like a pretty simple simple python script:
-------------------------------------------------------
#!/bin/env python
def spew(source_file_name,
		output_file_names,
		block_size = 8*1024):
	source = file(source_file_name, "rb")
	output_files = [file(s, "wb") for s in output_file_names]
	s = source.read(block_size)
	while s:
		for outfile in output_files:
			outfile.write(s)
		s = source.read(block_size)

	for outfile in output_files:
		outfile.close()
	source.close()

if __name__ == '__main__':
	from sys import argv
	if len(argv) > 2:
		spew(argv[1], argv[2:])
	else: #print some usage/help
		print "%s source_file dest_file1 [dest_file2 [dest_file3 
[...]]]" % argv[0]
-------------------------------------------------------

Just call it with

bash> cd /mnt
bash> python spew.py file1.txt usb1/file1.txt usb2/file2.txt

for as many destinations as you want.  Adjust the blocksize as 
you see fit:

	spew(argv[1], argv[2:], 1024*1024)

or whatever your favorite blocksize is.

It's not terribly graceful about error-checking, or prompting if 
any of the files exist previously, or if it can't write to any of 
the output files because for any reason (full disk, 
write-protected disk, destination path doesn't exist, etc). 
Thus, if there's a problem, you'll get a pretty backtrace that 
will allow you to solve all your problems ;)

-tkc








More information about the Python-list mailing list