reading/writing files in windows
sameer
sameer_ at email.com
Tue Jun 10 13:16:54 EDT 2003
why would it need to be in binary mode? I create all files in 'a'
append mode. All my code reads the file only in 'r' mode. By
"jumbled" I you simply mean the lines are all joined together, as
though there were no linebreaks at all.
the code that generates the contents of the file
LINE_SEP = '\n'
def publish_formatted_error_report(retrieved_email_obj,
error_msg_list):
published_object =
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%s'%LINE_SEP
published_object +=
'------------HEADER-----------------------%s'%LINE_SEP
published_object +=
'Source:%s%s'%(retrieved_email_obj.msg_from,LINE_SEP)
published_object +=
'Date:%s%s'%(retrieved_email_obj.msg_date,LINE_SEP)
published_object +=
'Time:%s%s'%(retrieved_email_obj.msg_time,LINE_SEP)
published_object += 'Primus
Header:%s%s%s'%(retrieved_email_obj.primus_header, LINE_SEP, LINE_SEP)
published_object +=
'-----------ERRORS-----------------------%s'%LINE_SEP
for error in error_msg_list:
published_object += "%s%s"%(error,LINE_SEP)
published_object += '%s-----------ORIGINAL
MESSAGE-------------%s'%(LINE_SEP,LINE_SEP)
published_object += "%s%s"%(retrieved_email_obj.msg_body,LINE_SEP)
published_object +=
'%s----------CORRECTED---------------------%s%s%s%s'%(LINE_SEP,
LINE_SEP,
LINE_SEP, LINE_SEP, LINE_SEP)
return published_object
The part that reads it back:
def _extract_published_retrieved_email_object(published_object, v=0):
header = {}
lines = string.split(published_object, LINE_SEP)
lines = [string.replace(line, '\r','') for line in lines]
lines = [string.replace(line, LINE_SEP,'') for line in lines]
lines = filter(None, lines)
if v:
print "published object string"
print lines
# extract header
if string.find(lines[0],'HEADER') == -1:
raise "Illegal Header: %s"%lines[0]
if string.find(lines[1],'Source:') == -1:
raise "Illegal Header (Source Header Not Found): %s"%lines[1]
msg_from = lines[1][string.find(lines[1],':')+1:]
if string.find(lines[2],'Date:') == -1:
raise "Illegal Header (Date Header Not Found): %s"%lines[2]
msg_date = lines[2][string.find(lines[2],':')+1:]
if string.find(lines[3],'Time:') == -1:
raise "Illegal Header (Time Header Not Found): %s"%lines[3]
msg_time = lines[3][string.find(lines[3],':')+1:]
if string.find(lines[4],'Primus Header:') == -1:
raise "Illegal Header (Primus Header Not Found): %s"%lines[4]
msg_primus_header = lines[4][string.find(lines[4],':')+1:]
if v:
print "Extracted Message Headers..."
print "From: ",msg_from, ' ', "Date: ", msg_date, ' ', "Time:
",msg_time, ' ', "Primus Header: ", primus_header
msg_body = ''
remaining_lines = lines[5:]
body_found = 0
for line in remaining_lines:
if string.find(line,
'----------CORRECTED---------------------') != -1:
body_found = 1
continue
if not line:
body_found = 0
break
if body_found:
msg_body += string.upper(line) + LINE_SEP
print "Msg Body"
print msg_body
#(msg_to, msg_from, msg_time, msg_subject, msg_body, msg_date)
return Retrieved_Email('', msg_from, msg_time, '', msg_body,
msg_date, primus_header=msg_primus_header)
def extract_formatted_error_report(v=0):
try:
error_string = open(CORRECTED_ERRORS_FILE,'r').read()
except IOError:
error_string = ''
message_list = string.split(error_string,
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
if v:
for i in message_list:
print "raw message is >>> %s"%i
retrieved_email_list=[]
single_msg_list = []
new_msg_list = []
# for i in message_list:
# single_msg_list=string.split(i,LINE_SEP)
# single_msg_list = filter(None, single_msg_list)
# new_msg_list.append(string.join(single_msg_list,LINE_SEP))
# new_msg_list = filter(None, new_msg_list)
# if v:
# print "[Status] new msg list:"
# print new_msg_list
for i in new_msg_list:
retrieved_email_list.append(_extract_published_retrieved_email_object(i))
return retrieved_email_list
Peter Hansen <peter at engcorp.com> wrote in message news:<3EE5D4AF.142ABB32 at engcorp.com>...
> sameer wrote:
> >
> > Of course the OS implied is windows (2000 Professional to be exact).
> > Moving across a network means that I am dragging the file from my
> > computer to shared folder on another computer, both computers being in
> > the same domain. This is just a plain text file. Then I open the
> > file that I transferred using notepad, make some changes to it and
> > save and there are no linebreaks at all in the text. all the lines
> > are jumbled together.
> >
> > sameer_ at email.com (sameer) wrote in message news:<6bd9f01b.0306090825.37845495 at posting.google.com>...
> > > I create files in windows using python. I have tried using \n and
> > > \r\n as the return character and they both work fine. It's when I
> > > move a text file, written by my python program, across a network, open
> > > the file and save to it that all the text becomes jumbled up, and all
> > > return characters are lost. How can I make it so this doesn't happen?
>
> Sorry, you'll have to do better. You still haven't posted any
> example code showing how you "create files ... using python".
>
> Also, by "jumbled" do you simply mean the lines are all joined
> together, as though there were no linebreaks at all? It's better
> to be precise when describing a problem that you hope others will
> help you debug.
>
> Do you have any program which will let you view the binary contents
> of the file after the transfer?
>
> Instead of "dragging" the file, can you use the copy command instead,
> both with the /A option and with the /B option?
>
> Are you perhaps having a problem with "binary mode" or not using
> binary mode when you create the file in the first place? You said
> you tried both \n and \r\n but without example code, no one can
> possibly know what you are really trying to do.
>
> -Peter
More information about the Python-list
mailing list