Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:35:04+00:00 2026-05-15T15:35:04+00:00

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

  • 0
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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:35:05+00:00Added an answer on May 15, 2026 at 3:35 pm

    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
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

catopen is failing to open same cat file in different servers, with same setup.
$ cat file.txt one two three $ cat file.txt | sed s/one/1/ 1 two
I do not want: $ cat file > dummy; $ cat header dummy >
file.txt contains: ##w## ##wew## using mac 10.6, bash shell, the command: cat file.txt |
I am having trouble with this simple task: cat file | grep -E ^[0-9]+$
Is there a case of ... or context where cat file | ... behaves
I am using sed to concatenate the contents of a file as: cat source.c
abc.dat file contains lines 1 2 3 code is tradeline= for line in $(cat
Once I had a file, to see the contents I used cat command, say
I have a file with the following information: dog<> cat<> cow<> bird<> tiger<> lion<>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.