gpt4 book ai didi

Python file.read() 在后台获取比必要更多的数据

转载 作者:行者123 更新时间:2023-11-28 19:46:13 25 4
gpt4 key购买 nike

cat file_ro.py 
import sys
def file_open(filename):
fo=open(filename,'r')
fo.seek(7)
read_data=fo.read(3)
fo.close()
print read_data
file_open("file.py")

但是strace说

readlink("file_ro.py", 0x7fff31fc7ea0, 4096) = -1 EINVAL (Invalid argument)
getcwd("/home/laks/python", 4096) = 18
lstat("/home/laks/python/file_ro.py", {st_mode=S_IFREG|0755, st_size=150, ...}) = 0
stat("file_ro.py", {st_mode=S_IFREG|0755, st_size=150, ...}) = 0
open("file_ro.py", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0755, st_size=150, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa671a6c000
fstat(3, {st_mode=S_IFREG|0755, st_size=150, ...}) = 0
lseek(3, 0, SEEK_SET) = 0
read(3, "import sys\ndef file_open(filenam"..., 128) = 128
read(3, "ile_open(\"file.py\")\n\t\n", 4096) = 22
close(3) = 0
munmap(0x7fa671a6c000, 4096) = 0
stat("file_ro.py", {st_mode=S_IFREG|0755, st_size=150, ...}) = 0
open("file_ro.py", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0755, st_size=150, ...}) = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff31fc9e30) = -1 ENOTTY (Inappropriate ioctl for device)
fstat(3, {st_mode=S_IFREG|0755, st_size=150, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa671a6c000
lseek(3, 0, SEEK_CUR) = 0
read(3, "import sys\ndef file_open(filenam"..., 4096) = 150
lseek(3, 150, SEEK_SET) = 150
read(3, "", 4096) = 0
close(3) = 0
munmap(0x7fa671a6c000, 4096) = 0
open("file.py", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0755, st_size=305, ...}) = 0
fstat(3, {st_mode=S_IFREG|0755, st_size=305, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa671a6c000
lseek(3, 0, SEEK_SET) = 0
read(3, "import ", 7) = 7
read(3, "sys\ndef file_open(filename):\n\t\"\""..., 4096) = 298
close(3) = 0
munmap(0x7fa671a6c000, 4096) = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa671a6c000
write(1, "sys\n", 4sys
) = 4
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x306140efa0}, {0x306d10b2b0, [], SA_RESTORER, 0x306140efa0}, 8) = 0
close(5) = 0
munmap(0x7fa671952000, 4096) = 0
exit_group(0)

如你所见-

read(3, "import sys\ndef file_open(filenam"..., 4096) = 150

为什么当程序说只读取 3 个字节时 read() 返回 150 个字节?

最佳答案

由于您正在读取另一个 py 文件,因此事情变得困惑,但内置函数似乎忽略了您传递给 read() 的值,并缓冲了其余值。也许尝试使用 os.read() 代替?

file_ro.py:

import sys
def file_open(filename):
fo=open(filename,'r')
fo.seek(7)
read_data=fo.read(3)
fo.close()
print read_data
file_open("zzz")

zzz:

12345678901234567890123456789012345678901234567890

跟踪:

...
open("zzz", O_RDONLY|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=51, ...}) = 0
fstat64(3, {st_mode=S_IFREG|0644, st_size=51, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb73fb000
_llseek(3, 0, [0], SEEK_SET) = 0
read(3, "1234567", 7) = 7
read(3, "89012345678901234567890123456789"..., 4096) = 44
close(3) = 0
...

您可以指定要打开的缓冲区的大小('zzz', buffering=0),或者我使用了 os 模块,可以根据需要更严格地控​​制文件读取:

文件_ro2.py:

import sys, os
def file_open(filename):
fo=os.open(filename, os.O_RDONLY)
os.lseek(fo, 7, 0)
read_data=os.read(fo, 3)
os.close(fo)
print read_data
file_open("zzz")

strace2:

...
open("zzz", O_RDONLY|O_LARGEFILE) = 3
_llseek(3, 7, [7], SEEK_SET) = 0
read(3, "890", 3) = 3
close(3) = 0
...

关于Python file.read() 在后台获取比必要更多的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211569/

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