School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,365 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,431 people online right now. Registration is fast and FREE... Join Now!




How to detect if a text file has nothing in it.

 

How to detect if a text file has nothing in it.

xZachtmx

16 Jun, 2009 - 03:03 AM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 38



Thanked: 1 times
My Contributions
Ok here is my code for an alarm setter... once i have my gui it will all be using the interface but right now here it is:

CODE


import sys

#Zach's alarm setting module for the alarm program.
file = open("text.txt","r")
line = file.readline()
if line == (""):#<--------------------- code i need help with
    print("You dont have your alarm set.")
    response = raw_input("Would you like to set it now? [y / n ]")
    if response == ("n"):
        file.close()
        sys.exit()



it dosent seem to detect that there is nothing in the text file... is there something else i need?


User is offlineProfile CardPM
+Quote Post


dragon-slayer

RE: How To Detect If A Text File Has Nothing In It.

16 Jun, 2009 - 10:53 AM
Post #2

D.I.C Head
Group Icon

Joined: 6 May, 2007
Posts: 84



Thanked: 1 times
Dream Kudos: 100
My Contributions
CODE
import sys

#Zach's alarm setting module for the alarm program.
file = open("text.txt","r")
line = file.readline()

if len(line) == 1 or len(line) ==0:#<--------------------- code i need help with
    print("You dont have your alarm set.")
    response = raw_input("Would you like to set it now? [y / n ]")
    if response == ("n"):
        file.close()
        sys.exit()


The reason your code didn't operate as expected is as when you erase the data in a text file there is still white space, So what this know does is check the length if it's 1 then it is empty(1==whitespace) if it is 0 then it is empty but has been newly created I think this should work

This post has been edited by dragon-slayer: 16 Jun, 2009 - 10:57 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:43PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month