Python file.read() grabs more data than necessary, under the hood
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")
But strace says
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)
As you can see above -
read(3, "import sys\ndef file_open(filenam"..., 4096) = 150
Why read() returns 150 bytes when the program says to read only 3 bytes ?
Since you're reading in another py file things become confused, but it seems the built-in function ignores the value you pass to read(), and buffers the rest of the value. Maybe trying using os.read() instead?
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
strace:
...
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
...
You can specify the size of the buffer to open('zzz', buffering=0), or I used the os module and could more closely control the file reading as you wanted:
file_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
...
Buffering. To avoid that use open(filename, 'rb', bufsize=0).
精彩评论