Python : How to email attached Unix file in DOS format - python

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")

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

Related

remove remaining hex code in Pyc file

I have a project to send, where basically I have to send an email using python.
My code is complete so I was about to send it.
Because of the fact the module smtplib needs my email log in, I compiled my code so people could no see my email and password, however, even compiled, when we look at the hex code, we can still see my email and password (and some print)
Is there a way to compile so we have no information left after?
Thank you very much for your help and time !
Generally it is a bad idea to hold sensitive information in the code. There is no uniformly the best way to do it, but common practices to store credentials include:
in a separate code file not in your code base (local_settings.py, added to .gitignore)
in a separate config file outside of the project (e.g. json or yml)
environment variables (read using os.environ)
command line parameters
request as user input
a combination of all above

Reading attributes of .msg file

I am trying to read a .msg file to get the sender, recipients, and title.
I'm making this script for my workplace where I'm only allowed to install default python libraries so I want to use the email module to do this.
On the python website I found some examples of using the email module. https://docs.python.org/3/library/email.examples.html
Near the end of the page it talks about getting the sender, subject and recipient. I've tried using this code like this:
# Import the email modules we'll need
from email import policy
from email.parser import BytesParser
with open('test_email.msg', 'rb') as fp:
msg = BytesParser(policy=policy.default).parse(fp)
# Now the header items can be accessed as a dictionary, and any non-ASCII will
# be converted to unicode:
print('To:', msg['to'])
print('From:', msg['from'])
print('Subject:', msg['subject'])
This results in an output:
To: None
From: None
Subject: None
I checked the file test_email.msg, it is a valid email.
When I add a line of code
print(msg)
I get an output of a garbled email the same as if I opened the .msg file in notepad.
Can anybody suggest why the email module isn't finding the sender/recipient/subject correctly?
You are apparently attempting to read some sort of proprietary binary format. The Python email library does not support this; it only handles traditional (basically text) RFC822 / RFC5322 format.
To read Microsoft's OLE formats, you will need a third-party module, and some patience, voodoo, and luck.
Also, for the record, there is no unambigious definition of .msg. Outlook uses this file extension for its files, but it is used on other files in other formats as well, including also traditional RFC822 files.
(The second link attempts to link to the MS-OXMSG spec on MSDN; but Microsoft have in the past regarded URLs as some sort of depletable resource which runs out when you use it, so the link will probably stop working if enough people click on it.)

Changing the content of the file in python

Is it possible to have a script (written in Python) that can change a content of a certain text file according to a database records.
For example: I have a DB that has some records of different types and a script that retrieves a single record from the database and according to that record it automatically changes a content of the text file that is somewhere on the system (e.g. tempo_file.txt).
I know that Python can read from database, read from file and input to the file. But what kind of logic should I apply to overcome this problem? Thanks.
You have to realize, when read from I file, you actually read it to the memory. You can manipulate it in your program.
Then you can flush the bytes in your memory into the disk. AKA save it or write it. If you want to "automatically change something", I guess you have to just read from the file and write to the file that has the same name.
There really is no way to manipulate it on the hard disk.
what kind of logic should I apply to overcome this problem?
Well quite simply:
read from the database
open the text file
write to the text file
close the text file
lather, rinse, repeat

What is a good audio library for validating files in Python?

I'm already checking for content-type, size, and extension (Django (audio) File Validation), but I need a library to read the file and confirm that it is in fact what I hope it is (mp3 and mp4 mostly).
I've been here: http://wiki.python.org/moin/Audio/ but no luck. Been at this one for a while, am a bit lost in the woods. Relying on SO big time for this whole end of things...
Thanks in advance.
EDIT:
I'm already (in Django) using UploadedFile.content_type() :
"The content-type header uploaded with the file (e.g. text/plain or application/pdf). Like any data supplied by the user, you shouldn't trust that the uploaded file is actually this type. You'll still need to validate that the file contains the content that the content-type header claims -- "trust but verify."
So, I'm already reading the header. But how can I validate the actual content of the file?
If just checking the header isn't good enough, I'd recommend using mutagen to load the file. It should throw an exception if it's not correct.
FYI, I do not think your approach is very scalable. Is it really necessary to read every byte of the file? What is your reason for not trusting the file header?
You can call a unix sub-shell within python like this:
>>> filename = 'Giant Steps.mp3'
>>> import os
>>> type = os.system('file %s' % filename)
Giant Steps.mp3: ISO Media, MPEG v4 system, iTunes AAC-LC
** See man pages for more details on the 'file' command if you want to go this route.
See this post for other options
Use sndhdr
It does a little more than content-type. Reads the file and gets it's headers..of course this is still not foolproof..using ffmpeg is probably then the only option.

python adding gibberish when reading from a .rtf file?

I have a .rtf file that contains nothing but an integer, say 15. I wish to read this integer in through python and manipulate that integer in some way. However, it seems that python is reading in much of the metadata associated with .rtf files. Why is that? How can I avoid it? For example, trying to read in this file, I get..
{\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf460
{\fonttbl\f0\fswiss\fcharset0
Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl720\margr720\margb720\margt720\vieww9000\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
That's the nature of .RTF (i.e Rich Text files), they include extra data to define how the text is layed-out and formated.
It is not recommended to store data in such files lest you encounter the difficulties you noted. Would you go through the effort to parse this file and "recover" your one numeric value, you may expose your application to the risk of updated versions of the RTF format which may render the parsing logic partially incorrect and hence yield wrong numeric data for the application).
Why not store this info in a true text file. This could be a flat text file or preferably an XML, YAML, JSON file for example for added "forward" compatibility as your application and you may add extra parameters and such in the file.
If this file is a given, however, there probably exist Python libraries to read and write to it. Check the Python Package Index (PyPI) for the RTF keyword.
That's exactly what the RTF file contains, so Python (in the absence of further instruction) is giving you what the file contains.
You may be looking for a library to read the contents of RTF files, such as pyrtf-ng.

Categories

Resources