Checking if a text file is blank

E

elnoire

Greetings!

I've just started learning python, so this is probably one of those
obvious questions newbies ask.

Is there any way in python to check if a text file is blank?

What I've tried to do so far is:

f = file("friends.txt", "w")
if f.read() is True:
"""do stuff"""
else:
"""do other stuff"""
f.close()

What I *mean* to do in the second line is to check if the text file is
not-blank. But apparently that's not the way to do it.

Could someone set me straight please?
 
D

Dan Bishop

Greetings!

I've just started learning python, so this is probably one of those
obvious questions newbies ask.

Is there any way in python to check if a text file is blank?

What I've tried to do so far is:

f = file("friends.txt", "w")
if f.read() is True:
"""do stuff"""
else:
"""do other stuff"""
f.close()

What I *mean* to do in the second line is to check if the text file is
not-blank. But apparently that's not the way to do it.

Could someone set me straight please?

The flaw in your code is that "f.read() is True" doesn't do what you
think it does.

(1) The "is" operator is a test for object IDENTITY. Two objects can
be equal but distinct
list1 = list2 = [1, 2, 3]
list1 is list2 True
list1[-1] = 4
list2
[1, 2, 4]
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list1 is list2 False
list1[-1] = 4
list2
[1, 2, 3]

(2) Even if you used "f.read() == True", it still wouldn't work in
Python. Values can be true or false (in the context of an if or while
statement) without being equal to True or False.

Values that are equal to True:
True, 1, 1.0, (1+0j), decimal.Decimal(1)

Values that are true but not equal to True:
"spam", "1", (1,), [1], set([1]), {1: 2}, etc.

Values that are equal to False:
False, 0, 0.0, 0j, decimal.Decimal(0)

Values that are false but not equal to False:
"", (), [], set()

And even if you are sure that you're only dealing with values of 0 or
1, it's unnecessary to write "if x == True:". It's redundant, just
like "if (x == True) == True:" or "if ((x == True) == True) ==
True:". Just write "if x:". Or, in this specific case, "if
f.read():".

(3) While not affecting your program's correctness, it's rather
inefficient to read a gigabytes-long file into memory just to check
whether it's empty. Read just the first line or the first character.
Or use os.stat .
 
D

David

Is there any way in python to check if a text file is blank?

What I've tried to do so far is:

f = file("friends.txt", "w")
if f.read() is True:
"""do stuff"""
else:
"""do other stuff"""
f.close()

What I *mean* to do in the second line is to check if the text file is
not-blank. But apparently that's not the way to do it.

Could someone set me straight please?

You're opening your file in write mode, so it gets truncated. Add "+"
to your open mode (r+ or w+) if you want to read and write.

Here is the file docstring:

file(name[, mode[, buffering]]) -> file object

Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
writing or appending. The file will be created if it doesn't exist
when opened for writing or appending; it will be truncated when
opened for writing. Add a 'b' to the mode for binary files.
Add a '+' to the mode to allow simultaneous reading and writing.
If the buffering argument is given, 0 means unbuffered, 1 means line
buffered, and larger numbers specify the buffer size.
Add a 'U' to mode to open the file for input with universal newline
support. Any line ending in the input file will be seen as a '\n'
in Python. Also, a file so opened gains the attribute 'newlines';
the value for this attribute is one of None (no newline read yet),
'\r', '\n', '\r\n' or a tuple containing all the newline types seen.

'U' cannot be combined with 'w' or '+' mode.

Note: open() is an alias for file().

Also, comparison of a value with True is redundant in an if statement.
Rather use 'if f.read():'

David.
 
A

Andrew Lee

Greetings!

I've just started learning python, so this is probably one of those
obvious questions newbies ask.

Is there any way in python to check if a text file is blank?

What I've tried to do so far is:

f = file("friends.txt", "w")
if f.read() is True:
"""do stuff"""
else:
"""do other stuff"""
f.close()

What I *mean* to do in the second line is to check if the text file is
not-blank. But apparently that's not the way to do it.

Could someone set me straight please?


Along with the other posts ... consider using the lstat command to get
information about the file.


import os
print os.lstat("friends.txt")[6]


gives the size in bytes of friends.txt or throws an OSError if
friends.txt does not exist.

lstat is portable, it defaults to stat on Windows.
 
P

prashanth

Hi

Greetings!

Is there any way in python to check if a text file is blank?


obviously there are many ways, one simple way is to check length if its
None then the file is blank.

What I've tried to do so far is:

f = file("friends.txt", "w")
if f.read() is True:
"""do stuff"""
else:
"""do other stuff"""
f.close()

If checking the file is blank is what you are tying to do then why do
you open the file in the write mode.




Prashanth
 
E

evenrik

Greetings!

I've just started learning python, so this is probably one of those
obvious questions newbies ask.

Is there any way in python to check if a text file is blank?

What I've tried to do so far is:

f = file("friends.txt", "w")
if f.read() is True:
"""do stuff"""
else:
"""do other stuff"""
f.close()

What I *mean* to do in the second line is to check if the text file is
not-blank. But apparently that's not the way to do it.

Could someone set me straight please?

I use os.path.getsize(path) for this purpose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top