2

in a py module, I write:

outFile = open(fileName, mode='w')
if A:
    outFile.write(...)
if B:
    outFile.write(...)

and in these lines, I didn't use flush or close method. Then after these lines, I want to check whether this "outFile" object is empty or not. How can I do with it?

4
  • 5
    in the mode 'r' , you can't use write(). Commented Dec 20, 2012 at 15:55
  • 1
    when you say, "is empty", just what do you mean? do you mean "has the data I've written reached disk yet?" or maybe "After all of that, did I actually try to write anything?" Commented Dec 20, 2012 at 15:57
  • @TokenMacGuy Even I think he's talking about the data that is not flushed yet. Commented Dec 20, 2012 at 15:59
  • @TokenMacGuy I mean I want to ensure whether I've really write something into the file object, before I'm going to flush to disk. Commented Dec 21, 2012 at 0:52

4 Answers 4

4

There are a few problems with your code.

  1. You can't .write to a file that you opened with 'r'. You need to open(fileName, 'w').

  2. If A or B then you've certainly written to the file, so it's not empty!

Barring those. you can get the length of a file with

os.stat(outFile.fileno())

EDIT: I'll explain what flush does. Python is often used to do quite large amounts of file reads and writes, which can be slow. It is thus tweaked to make them as fast as possible. One way that is does so is to "buffer" such writes and then do them all in one big block: when you write a small string, Python will remember it but won't actually write it to the file until it thinks it should.

This means that if you want to tell whether you have written data to the file by inspecting the file, you have to tell Python to write all the data it's remembering first, or else you might not see it. flush is the command to write all the buffered data.

Of course, if you ask Python whether it's written anything to the file, say by inspecting the position in the file (.tell()), then it will know about the buffering.

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

5 Comments

re: #2, not neccesarily, outFile.write('') didn't write a thing!
Re: (2) -- even if what's written is nonempty, that doesn't mean st_size is nonzero yet. I just did a test, writing "z" to a file and then calling os.stat immediately, and got st_size=0. [P.S. I think os.stat accepts a path, not a fileno.]
Agreed with all comments =)
Well, because I haven't flushed yet, I think it did not really write to the disk. So getting file attribute may not be useful.
@DSM so how can I ensure whether the file object is written?
1

If you've already written to the file, you can use .tell() to check if the current file position is nonzero:

>>> handle = open('/tmp/file.txt', 'w')
>>> handle.write('foo')
>>> handle.tell()
3

This won't work if you .seek() back to the beginning of the file.

2 Comments

So, if I use file.seek(0,2) then file.tell() and tell() returns 0, does it mean the file is empty?
@ThunderEX: No, it just means that the current position in the file is 0.
1

You can use os.stat to get file info:

import os
fileSize = os.stat(fileName).st_size

1 Comment

But this wouldn't be from the file object
1
with open("filename.txt", "r+") as f:
    if f.read(): 
        # file isn't empty
        f.write("something")
        # uncomment this line if you want to delete everything else in the file
        # f.truncate()
    else:
        # file is empty
        f.write("somethingelse")

"r+" mode always you to read & write. "with" will automatically close file

1 Comment

You can have f.read(size) so that you don't load the entire file in memory just to check if it's empty, e.g. f.read(10)

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.