1 Replies - 965 Views - Last Post: 16 June 2009 - 11:53 AM Rate Topic: -----

#1 xZachtmx  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 125
  • Joined: 14-June 09

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

Post icon  Posted 16 June 2009 - 04:03 AM

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:


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?

Is This A Good Question/Topic? 0
  • +

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

#2 dragon-slayer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 06-May 07

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

Posted 16 June 2009 - 11:53 AM

 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 June 2009 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1