gpt4 book ai didi

python - 如何在磁带上写 EOF 标记 - Python

转载 作者:行者123 更新时间:2023-12-04 15:21:32 26 4
gpt4 key购买 nike

我在网上搜索了很多,但找不到用 Python 在磁带上写 EOF 标记的方法。

我有以下代码(通过 fcntl.ioctl 使用 Python)写入记录,但在每次 os.write 之后它不会写入 EOF,但会保留记录单个文件。本质上,我想将这些记录拆分为中间带有 EOF 标记的文件?

代码:

import os
import struct
import fcntl

MTIOCTOP = 0x40086d01 # Do a magnetic tape operation
MTSETBLK = 20
TAPEDRIVE = '/dev/st1'

fh = os.open(TAPEDRIVE, os.O_WRONLY )
fcntl.ioctl(fh, MTIOCTOP, struct.pack('hi', MTSETBLK, 0))
os.write(fh, b'a'*1024) #<- Does not add EOF mark after write
fcntl.ioctl(fh, MTIOCTOP, struct.pack('hi', MTSETBLK, 0))
os.write(fh, b'b'*2048)
fcntl.ioctl(fh, MTIOCTOP, struct.pack('hi', MTSETBLK, 0))
os.write(fh, b'c'*1024)
fcntl.ioctl(fh, MTIOCTOP, struct.pack('hi', MTSETBLK, 0))
os.write(fh, b'd'*2048)
os.close(fh)

磁带分析:

Commencing Reading Tape in Drive /dev/st1, blocksize = 32768
1024 2048 1024 2048
End of File Mark after 4 records
End of File Mark after 0 records
End of Tape
Tape Examination Complete, found 2 Files on tape`

我注意到 mtio.h 包含 MTWEOF here但我不确定如何通过 ioctl 实现它?

如有任何帮助,我们将不胜感激。

附言。我知道我可以使用 mt -f/dev/st1 weof n# 编写 EOF 标记,但我更愿意将其保留在 Python 中。

最佳答案

好的,在阅读了 mtio.h 手册页后,我解决了这个问题,希望它能对其他人有所帮助。

import os
import fcntl
import struct

MTIOCTOP = 0x40086d01 # Do a magnetic tape operation refer to mtio.h
#MTSETBLK = 20 # Set a block size?
MTWEOF = 5 # Define EOF mark variable refer to mtio.h
TAPEDRIVE = '/dev/st1' # Tape drive location

fd = os.open(TAPEDRIVE, os.O_WRONLY ) # Open device for write
#fcntl.ioctl(fd, MTIOCTOP, struct.pack('hi', MTSETBLK, 32768)) # Set a block size?

for _ in range(5):
os.write(fd, b'a'*1024) # Write some bytes
fcntl.ioctl(fd, MTIOCTOP, struct.pack('hi', MTWEOF, 1)) # Write end-of-file (1)

fcntl.ioctl(fd, MTIOCTOP, struct.pack('hi', MTWEOF, 2)) # Write end-of-tape (2)
os.close(fd)

磁带分析

Commencing Reading Tape in Drive /dev/st1, blocksize = 32768
1024 1024 1024 1024 1024
End of File Mark after 1 records
End of File Mark after 1 records
End of File Mark after 1 records
End of File Mark after 1 records
End of File Mark after 1 records
End of Tape
Tape Examination Complete, found 5 Files on tape

关于python - 如何在磁带上写 EOF 标记 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63185830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com