user1513192
user1513192

Reputation: 1153

File in memory, Python

I have a string of text that I would like to create a .txt file out of. I would not like to allow it to be accessible to the user (for security reasons), so I would like to store the .txt file in memory (if this is even possible).

For example: The string is:

'''
Username: Bob
Password: Coolness

'''

I would like to save this string as a .txt file into memory. Then send it to another program.

anotherprogram.exe mytxt.txt

I looked around, and I am wondering if doing this is possible with StringIO? It says " Read and write strings as files".. I am not sure, Please respond if you know how to do this in anyway.

Upvotes: 5

Views: 4031

Answers (4)

user1513192
user1513192

Reputation: 1153

Theres a module called wxpython that has a class called wx.FileSystem

Upvotes: 0

Meitham
Meitham

Reputation: 9670

Why don't you rely on the security offered by your OS (I guess your is Windows) to make the file only accessible to a user but not all users. Obviously anotherprogram.exe will have to be executed as a user who has read access to the file.

Upvotes: 0

mgilson
mgilson

Reputation: 309821

If the other program can read from standard input, then subprocess might be the way to go. Here's a stupid example where anotherprogram.exe is cat.

s='''
Username: Bob
Password: Coolness

'''

from subprocess import Popen,PIPE
p = Popen(['cat','-'], stdin=PIPE, stdout=PIPE)
stdoutdata, _ = p.communicate(s)

Many utilities that accept input from a file can read from stdin by passing - as the filename, but not all. Whether this works will depend heavily on what anotherprogram.exe actually is.

Upvotes: 3

jcklie
jcklie

Reputation: 4094

If you do not want to make it accessible for others, use encryption and hide the key or maybe use the user control system offered by the operating system. I would prefer the first.

You can create files in memory by using ram disks, but files created in this way do not offer security ( or just a little bit since it is volatile). There is a module for python called pyFilesystem which would do that for you.

But as far as I can think you can reach nearly every file on the disk as a user, therefore, it is difficult to prevent a eager user from finding it effectively.

The file need to be found for anotherprogram.exe has to be in the user scope, therefore, it must be somehow accesible for the user.

Upvotes: 1

Related Questions