Reading & Writing to Tape using Python

Albert Hofkamp hat at se-46.wpa.wtb.tue.nl
Tue Oct 8 07:29:31 EDT 2002


On Thu, 3 Oct 2002 18:59:34 +0100, Ricardo <nospam at here> wrote:
> I have been trying to write data directly to a tape drive using Python (on
> HP-UX and Linux).  However, unless the original data is small, I don't get
> it all when I attempt to read it.  I suspect this may be due to block size.

Quite likely, although I have never tried that with tapes.
With sockets, I experienced similar problems due to network-packet sizes.
I solved it by repeatedly reading/writing, until I get EOF (an empty
string) cq I run out of bytes to write.
I would expect that to work on a tape device as well.

> Can anybody point me to some example code for doing this?

Since you're asking :-)

def read_tape(fp):
    s=''
    while 1:
        t=fp.read()
        if t=='':
            break
        s=s+t
    return s
def write_tape(fp,d):
    while d!='':
        q=fp.write(d)
        assert q>0 # paranoia check
        d=d[q:]
-- 
Albert
-- 
Unlike popular belief, the .doc format is not an open publically available format.



More information about the Python-list mailing list