I need help please. this cgi program works fine only if the user puts
in more than 66 chars..i chopped the lines into 66 chars each so they would fit in the textarea the problem is that record comments() works perfectly for writing multiple lines and the \n char in commentlog.txt but for a single line i use recordcomment() and no matter how i tried it it wouldnt write the extra \n line....as u can see i desperately need that extra line that is a \n so parseposts() can put the posts in their correct orders. thanks for your help.

#!c:/Python26/python.exe
import os
import cgi
def getpostcount():
    f=open('C:/commentlog.txt', 'r')
    a=f.readlines()
    f.close()
    i=0
    for line in a:
        if line.startswith( "\n" ):
            i+=1
    return i
def clearold():
    f=open('C:/commentlog.txt', 'r')
    a=f.readlines()
    f.close()
    for line in a:
        a=a[1:]
        if line=="\n":
            break
    f=open('C:/commentlog.txt', 'w')
    for line in a:
        f.write(line)
    f.close()
def oneline():
    try:
        form=cgi.FieldStorage()
        q=form.getvalue("posts")
        if len(q)>66:
            return 1
    except:
        return 0
def parseposts(posts):
    b=[]
    c=[]
    for a in posts:
        if not a=="\n":
            a=a[:-1]
            b.append(a)
        else:
            c.append(b)
            b=[]
    return c
def getposts():
    f=open('C:/commentlog.txt', 'r')
    a=f.readlines()
    f.close()
    b=parseposts(a)
    return b


def postit():
    a=getposts()
    print "Content-type: text/html\r\n\r\n"
    print "<style type=\"text/css\">"
    print "h2 {"
    print "color : green;"
    print "}"
    print "textarea.input {"
    print "  color : black;"
    print "  background-color : #00f000;"
    print "}"
    print "body {"
    print "  background-color : black; } "
    print "</style>"
    print "<html>"
    print "<head>"
    print "<img src=\"  \" width=\"240\" height=\"230\" align=\"right\">"
    print "</head>"
    print "<body>"
    print "<h2>This is my second page anyone can post comments anonymously<br>"
    print "<form action=\"comments.py\" method=\"POST\">"
    print "<textarea class=\"input\"name=\"posts\" rows=\"10\" cols=\"65\">"
    print "</textarea>"
    print "<input type=\"submit\" value=\"post\">"
    print "</form>"
    print "<hr>"
    print "</body>"
    print "</html>"
    for post in a:
        print "<textarea readonly class=\"input\" rows=\"10\" cols=\"65\">"
        for line in post:
            print line
        print "</textarea>"
        print "<hr>"
def parsepost(post):
    a=[]
    b1=post
    while len(b1)>66:
        b2=b1[:65]
        b1=b1[65:]
        b2+="\n"
        a.append(b2)
    b1+="\n"
    a.append(b1)
    return a
def getpost():
    try:
        form=cgi.FieldStorage()
        q=form.getvalue("posts")
        s=q
        if len(s)>66:
            s=parsepost(q)
        return s
    except:
        return 0
def recordcomment(post):
    f=open('C:/commentlog.txt', 'a')
    f.write(post)
    f.write("\n")
    f.close()

def recordcomments(posts):
     f=open('C:/commentlog.txt', 'a')
     for a in posts:
         f.write(a)
     f.write("\n")
     f.close()


def main():
    comment=getpost()
    o=oneline()
    if comment:
        count=getpostcount()
        if count>=20:
            clearold()
        if o==None:
            comment+="\n"
            recordcomment(comment)
        else:
            recordcomments(comment)


try:    
    main()
    postit()

except:


    print "Content-type: text/html\r\n\r\n"
    print"err"

Recommended Answers

All 4 Replies

Well, seeing as you didn't read this simple post about using code tags, your Python has no indentation. Brilliant. I'm not going to try to fix your code's indentation so that I can begin to help with your problem. If you can post it with those helpful tags around it, I'll take a look.

sorry this is my first time posting.didnt know about the Code thing

#!c:/Python26/python.exe
import os
import cgi
def getpostcount():
    f=open('C:/commentlog.txt', 'r')
    a=f.readlines()
    f.close()
    i=0
    for line in a:
        if line.startswith( "\n" ):
            i+=1
    return i
def clearold():
    f=open('C:/commentlog.txt', 'r')
    a=f.readlines()
    f.close()
    for line in a:
        a=a[1:]
        if line=="\n":
            break
    f=open('C:/commentlog.txt', 'w')
    for line in a:
        f.write(line)
    f.close()
def oneline():
    try:
        form=cgi.FieldStorage()
        q=form.getvalue("posts")
        if len(q)>66:
            return 1
    except:
        return 0
def parseposts(posts):
    b=[]
    c=[]
    for a in posts:
        if not a=="\n":
            a=a[:-1]
            b.append(a)
        else:
            c.append(b)
            b=[]
    return c
def getposts():
    f=open('C:/commentlog.txt', 'r')
    a=f.readlines()
    f.close()
    b=parseposts(a)
    return b


def postit():
    a=getposts()
    print "Content-type: text/html\r\n\r\n"
    print "<style type=\"text/css\">"
    print "h2 {"
    print "color : green;"
    print "}"
    print "textarea.input {"
    print "  color : black;"
    print "  background-color : #00f000;"
    print "}"
    print "body {"
    print "  background-color : black; } "
    print "</style>"
    print "<html>"
    print "<head>"
    print "<img src=\"  \" width=\"240\" height=\"230\" align=\"right\">"
    print "</head>"
    print "<body>"
    print "<h2>This is my second page anyone can post comments anonymously<br>"
    print "<form action=\"/comments.py\" method=\"POST\">"
    print "<textarea class=\"input\"name=\"posts\" rows=\"10\" cols=\"65\">"
    print "</textarea>"
    print "<input type=\"submit\" value=\"post\">"
    print "</form>"
    print "<hr>"
    print "</body>"
    print "</html>"
    for post in a:
        print "<textarea readonly class=\"input\" rows=\"10\" cols=\"65\">"
        for line in post:
            print line
        print "</textarea>"
        print "<hr>"
def parsepost(post):
    a=[]
    b1=post
    while len(b1)>66:
        b2=b1[:65]
        b1=b1[65:]
        b2+="\n"
        a.append(b2)
    b1+="\n"
    a.append(b1)
    return a
def getpost():
    try:
        form=cgi.FieldStorage()
        q=form.getvalue("posts")
        s=q
        if len(s)>66:
            s=parsepost(q)
        return s
    except:
        return 0
def recordcomment(post):
    f=open('C:/commentlog.txt', 'a')
    f.write(post)
    f.write("\n")
    f.close()

def recordcomments(posts):
     f=open('C:/commentlog.txt', 'a')
     for a in posts:
         f.write(a)
     f.write("\n")
     f.close()


def main():
    comment=getpost()
    o=oneline()
    if comment:
        count=getpostcount()
        if count>=20:
            clearold()
        if o==None:
            comment+="\n"
            recordcomment(comment)
        else:
            recordcomments(comment)
    else:
        continue


try:    
    main()
    postit()

except:


    print "Content-type: text/html\r\n\r\n"
    print"err"
def recordcomment(post):
    f=open('C:/commentlog.txt', 'a')
    f.write(post)
    f.write("\n")
    f.close()

It seems to me that you're trying to use the file.write() method in the same manner as print . correct me if I'm wrong, but you want a blank line (new line character \n ) in between each comment in the comments file?

If so you'll simply need an additional newline character on your write statement. It'd probably be easier if you just did a single write statement though, like this: f.write( '%s\n\n' % post ) . This statement inserts the contents of post into a string followed by two new-line characters.

Hopefully that's what you're looking for, if not try asking your question in a cohesive and clear manner.

One more thing. Are you using a=a[1:] to remove the new line character from the end of your data? This is a risky move, and probably not the best method. You'd be better off using a.strip() or a.rstrip() to take off any whitespace from the beginning and end, or simply the end (respectively) of your string. This way, if there is no newline character, you don't remove the last character of the message.

i dont know if its a bug or whats goin on...no matter how many \n charachters i put in there, it doesnt write them to the file in recordcomment()...but recordcomments() works fine..so i even tried to make the single line comments into a list and pass it to recordcomments() but that didnt even work...this is very frustrating...and thanx for the strip pointer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.