LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2004, 09:21 AM   #1
indian
Member
 
Registered: Aug 2004
Posts: 137

Rep: Reputation: 15
what's wrong with this small code ?


Hi what is wrong with this code...I have written a small counter in python
for my website.I am keeping the numbe in a file.than whenever someone visit the page I open the count.txt file..add one to that num(after converting it into int) and than I print that number.
Again I open the file and than I want to write that incremented number in that file but it is giving errors.Where is the code wrong(I think it is in the conversion of int to str...)
Thanks in anticipation...

#!/usr/bin/python
import cgi

def main():
print "Content-type:text/html\n"

fh=open("count.txt")
data=fh.read()
fh.close()
data=int(data)+1

print "<html><body>Ok so this is counter:", data," </body></html>"
fh=open("count.txt")
str(data) # I am converting int data to string...
fh.write(data) # I am writing string into the count.txt file
fh.close()

main()
 
Old 08-18-2004, 09:51 AM   #2
peace
Member
 
Registered: Jul 2003
Location: Canada
Posts: 214

Rep: Reputation: 31
Hi,

When you open your file:

Quote:
fh=open("count.txt")
The syntax for this is, open(filename[,mode])

If mode is omitted, it will default to read-only. This will raise an exception if you try to write to it.

Try:

fh = open("count.txt","w+")

And remember for next time, it helps to post the error message
 
Old 08-18-2004, 11:12 AM   #3
indian
Member
 
Registered: Aug 2004
Posts: 137

Original Poster
Rep: Reputation: 15
First of all...Thanks a lot..and secondly Damm...it was such a stupid mistake...In the future I will give a better attempt and only than I will post my questions..anyway thanks again...My program is running..now...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
What is wrong with the following C code? indian Programming 17 12-04-2005 09:01 AM
What's difference between the two small blocks of c++ code clinux_rulz Programming 9 12-04-2005 08:39 AM
What is wrong with this C code ? indian Programming 8 04-16-2005 04:42 AM
small errors in kernel code (2.4.22) Robert0380 Linux - General 2 10-06-2003 09:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration