Cristi Constantin
Cristi Constantin

Reputation: 544

Python execute a file, then delete it

Hi Stackoverflow experts, this is a hard one, for Python programmers.

I'm working on an encrypted SQLITE3 container, written in Python. The final target are users, not developers.

All the functions are working as expected, but my problem is with a function to EXECUTE files from the container.

How the EXECUTE works:

  1. i create a temporary folder with tempfile.mkdtemp
  2. i decrypt the file from the container, into that folder (from what i know, this is the only way to open a file)
  3. A: on Windows, i execute the file with os.system('file_name & exit'), so the file is opened with the default app (ex: images with fax viewer by default, music with media player by default, etc, etc). This is usually blocking.
  4. B: on Ubuntu/ Mint (it's all i tested so far), i execute with subprocess.check_output(['xdg-open', file_name]), but this is not always blocking (ex: when you open one text file by default with Gedit, if Gedit is already open, the function returns immediately).
  5. i delete the file
  6. i delete the temporary folder, because it's not automatically deleted

Ok, so steps 1 and 2 are ok. I would prefer NOT to write the file on HDD, for example i want to execute it from memory, but you need to tell each app WHERE to open a file from, it must be a path from HDD, right?

The most secure would be to implement my own internal viewers for text, images, music, video, so i don't need to export the data on HDD. But this is not realistic :)

So the problem are steps 4 and 5. How do i know WHEN to delete the file after execution, if the execution is not blocking ?...

I tried all the functions from subprocess (wait, communicate, check_call) and os.system, to make the execution blocking... Maybe there are other functions to wait for processes to finish ?...

Maybe there is a way to delete the temporary files automatically ? Or i should make a list on runtime and delete them when the program closes...

Maybe there is a way to check if a file is opened by some app ? Ex: "my-pic.jpg" is opened by Image-Viewer, so i cannot delete it yet, but i will check again in a few seconds and if no other app uses it, i can delete it. // Maybe with "ps ax | grep my-pic" i can find if the file is open. But this doesn't work on Windows.

Any ideas, any sugestions ?

Thank you very much !

Upvotes: 1

Views: 838

Answers (1)

robert
robert

Reputation: 3615

Some ideas:

  • make the user tell your application when he has no longer need of the temporarily extracted files
  • query the Windows API to find out which processes are using files in a certain folder (e.g. your temp folder)
  • in any case, try to delete the file shortly after opening it; this might often work, because many applications do not protect the files that they're using, i.e. they allow other applications to modify/delete a file even while they're using it
  • have a look at PendMoves/MoveFile. Maybe you can implement something similar for your application.
  • create a small RamDisk for your application and use it to store the temporary files in
  • use TrueCrypt or similar tools for mounting encrypted containers (managed by your application) to store the temporary files in and - if necessary - force un-mounting of these containers.

Upvotes: 1

Related Questions