open a file in python

J

jayshree

pk = open('/home/jayshree/my_key.public.pem' , 'rb').read()

Please tell me how to open a file placed in any directory or in same
directory.

After opening this file i want to use the contain (public key ) for
encryption





thanks
 
K

Kushal Kumaran

pk = open('/home/jayshree/my_key.public.pem' , 'rb').read()

Please tell me how to open a file placed in any directory or in same
directory.

After opening this file i want to use the contain (public key ) for
encryption

Does the code you've put into your message not read that file? If you
get an exception, copy-paste in the traceback message you get into
your mail.
 
J

jayshree

Does the code you've put into your message not read that file?  If you
get an exception, copy-paste in the traceback message you get into
your mail.


try:
pk = open('/home/jayshree/my_key.public.pem' , 'rb').read()
except IOError:
print "Error: can\'t find file or read data"
else:
print "reading from file successfully"
 
D

Diez B. Roggisch

jayshree said:
try:
pk = open('/home/jayshree/my_key.public.pem' , 'rb').read()
except IOError:
print "Error: can\'t find file or read data"
else:
print "reading from file successfully"

Erm - so it doesn't give an error - which means you have successfully opened
a file, and read it's contents.

So what exactly is your problem?

Diez
 
P

Piet van Oostrum

jayshree said:
j> pk = open('/home/jayshree/my_key.public.pem' , 'rb').read()

By the way, a PEM file is a text file, no reason to open it in binary
mode. Just replace the 'rb' with 'r' or leave it out.
 
P

Piet van Oostrum

I have already answered this question in a previous thread. It is not
very helpful if you repeat asking the same or similar questions in
different threads, especially if you don't take notice of the answers
given. I will repeat it once here:

load_pub_key requires a file name, not the contents of the file.
So use rsa = M2Crypto.RSA.load_pub_key('my_key.public.pem') and leave
the open line out.
 

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
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top