我需要在我知道路径的文件中的特定位置打印字节。所以我在" rb"中打开默认文件模式,然后我需要知道15位置的字节。这是可能的吗?
答案 0 :(得分:4)
以下是seek
:
with open('my_file', 'rb') as f:
f.seek(15)
f.read(1)
答案 1 :(得分:1)
另一种方法是读取整个文档并对其进行切片:
首先阅读文件的内容:
file = open('test.txt', 'rb')
a = file.read()
然后取出所需的值:
b = a[14]
然后不要忘记关闭文件:
file.close()
或者自动关闭:
with open('test.txt', 'rb') as file:
a = file.read()
b = a[14]