Using .seek(0) to read.
Amy G
amy-g-art at cox.net
Fri Jan 9 19:37:24 EST 2004
"Peter Otten" <__peter__ at web.de> wrote in message
news:btjbot$f56$00$1 at news.t-online.com...
> You can open the file in "r+" mode and then f_vm.seek(0) before writing.
> However, from the above example I can not see why you even bother to check
> the old contents and don't just write the new data.
>
> Peter
I got this respnse on a prior post. Peter was correct about not needing to
compare the old contents, and I was able to easily fix my code by taking out
any read.
However, now I need to open the file in "r+" mode and do a
<filename>.seek(0) on it. I am opening up the .procmailrc file for each
user in a database that has a "save_junkmail" == 1 and inserting some lines.
They need to be inserted in the correct spot and each user has a different
number of lines in their .procmailrc. Everything prints out correctly. I
now want to write the lines instead of printing to stdout. If I set up the
open() statement to say:
procmailrc_file = open("/home/%s/.procmailrc" %userid, 'r+')
How do I then get the values with .seek(0)? Is it simply
file_in_list = procmailrc_file.seek(0) ....?
Is there a better way to do this?
*********************CODE
SNIPPET****************************************************
c.execute("""SELECT save_junkmail, userid from users where save_junkmail =
'1'""")
d = c.fetchall()
if (hasElements(d) == 1):
for save_junkmail, userid in d:
#IS JUNKMAIL OFF - IF IT IS TURN IT ON. ELSE DO NOTHING
junkmail_off = os.popen("/bin/ls /home/%s/ | grep spam$" %userid)
if (junkmail_off):
junk_off = stripLs(junkmail_off.readlines())
#os.system("/bin/mv /home/%s/%s /home/%s/mail" %(userid, item,
userid))
procmailrc_file = open("/home/%s/.procmailrc" %userid, 'r')
procmailrc = stripLs(procmailrc_file.readlines())
x = procmailrc.index("| /usr/local/bin/spamc")
procmailrc.insert((x+2), ":0:\n* ^X-Spam-Status:
Yes\n$HOME/probably-spam\n")
procmailrc.insert((x+2), ":0:\n* ^X-Spam-Level:
\*\*\*\*\*\*\*\*\*\*\*\*\*\n$HOME/almost-certainly-spam\n")
for line in procmailrc:
print line
****************************************************************************
***********
Thanks for your help.
More information about the Python-list
mailing list