1

I have use the following code to read a .txt file:

f = os.open(os.path.join(self.dirname, self.filename), os.O_RDONLY)

And when I want to output the content I use this:

os.read(f, 10);

Which means that this method reads 10 bytes from the beginning of the file on. While I need to read the content as much as it is, using some values such as -1 and so. What should I do?

1
  • 1
    Why you don't use the open() built-in function and read the file line by line ? Commented Mar 15, 2014 at 10:58

1 Answer 1

2

You have two options:

  1. Call os.read() repeatedly.

  2. Open the file using the open() built-in (as opposed to os.open()), and just call f.read() with no arguments.

The second approach carries certain risk, in that you might run into memory issues if the file is very large.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.