Reputation: 630
On a Unix server, I am using smtplib in python to send an email to myself ; the email also contains a unix file attachment. I use outlook client to view the email and when I open the file, it does not display correctly due to differences in Unix and DOS format.
Is there anyway using smtplib to send the Unix file in DOS format ?
I do not want to use unix2dos as I do not want to create/modify files on the filesystem.
Editing the question to include changes based on suggestions from senior members
Since I have been asked to modify the file, need to know if there is a simpler way to do that. I am not well versed with Python so please bear with me. I have tried a few variations of the following but none have worked. My requirement is that I do not want to write to the file system. I want to save the changes into a variable in memory.
import string fo=open(filename,"r") filecontent=fo.readlines() for line in filecontent: line = string.replace(line,"\n","\r\m")
Upvotes: 0
Views: 184
Reputation: 52030
This is only a variation around the first comment on your question:
with open(filename, 'r') as f:
content = f.read().replace('\n', '\r\n')
After that, you have in the variable content the ... content of your file, with newlines replaced. In addition, using the with
construct ensure your file is properly closed after reading.
Please note it is your responsibility to ensure that the file is "small enough" to hold in memory. If not sure, you could read line by line as you proposed yourself. That being said, I'm not quite sure to understand what was wrong with that at first...
Upvotes: 1