Mostafa Talebi
Mostafa Talebi

Reputation: 9183

Reading a .txt file in python

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?

Upvotes: 1

Views: 148

Answers (1)

NPE
NPE

Reputation: 500733

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.

Upvotes: 2

Related Questions