Using .seek(0) to read.

A

Amy G

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.
 
T

Tim Roberts

Amy G said:
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.

You can't easily insert data in the middle of a text file. What you need
to do is create a NEW file, and copy the data from OLD to NEW, adding your
new lines at the appropriate spot. Then close both files, and rename them
into place:
os.remove( '.procmailrc.was' )
os.rename( '.procmailrc', '.procmailrc.was' )
os.rename( '.procmailrc.new', '.procmailrc' )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top