0

I'm trying to write a chatting app that when run the first time deletes itself from the current position. Now the code is:

import os
import sys


file=sys.argv[0]
os.remove(file)

if i run it as .py file, it works just fine. Anyway if compile it with pyinstaller, when run by terminal it raises:

Traceback (most recent call last):
  File "tests.py", line 6, in <module>
PermissionError: [WinError 5] Denied access: 'tests.exe'
[14320] Failed to execute script 'tests' due to unhandled exception!

nothing changes if i run it as administrator or assigning the file permissions with os.chmod. I have python 3.10, i tried both with python 3.9 and 3.10 and it does not work. I even tried running the command del with the subprocess module with the same result.

vik
  • 19
  • 2
  • If you have Windows could make an executable with ```zipapp``` module - be interesting to see if that works. https://docs.python.org/3/library/zipapp.html – InhirCode Feb 22 '22 at 21:39
  • great idea but sadly i need a .exe file. Just can’t figure out why this does not work, i’m starting to think it’s a weird bug. – vik Feb 22 '22 at 21:41
  • It could be a weird bug in pyinstaller associated with the string that is assigned to ```file``` such that the ```\\``` in the path name is not being recognised by Windows. (just throwing out ideas) does pyinstaller change path name stirngs?? unlikely I guess – InhirCode Feb 22 '22 at 21:51
  • Last idea :) if you create executable with zipapp could rename final result (if necessary) with exe extension since a Windows executable is presumably executable and the same format. – InhirCode Feb 22 '22 at 22:11
  • thought a bit more that Windows PermissionError sometimes happens when the program is already open. There is this link with a variety of suggestions. https://stackoverflow.com/questions/26091530/permissionerror-winerror-5-access-is-denied-python-using-moviepy-to-write-gif – InhirCode Feb 23 '22 at 20:11
  • sys.argv[0] is the program argument and so it would have removed the program file when run as ```py``` file ??? ```print(sys.srg)``` would verify this. Should it be sys.argv[1]?? – InhirCode Feb 23 '22 at 20:15
  • yeah like, it actually works if i run it as .py, it deletes the file, which is what i want. Before that i would run a **copy** command to copy that to another directory. I checked the link you sent by the most of the people just says there is a problem with permission but there isn't. I neither think it's a problem of double backslash because actually sys.argv[0] returns a simple path. That makes no sense. – vik Feb 23 '22 at 20:53
  • have put an answer to what I've come to about it - hope that helps – InhirCode Feb 24 '22 at 01:47

1 Answers1

0

This works as .py file because the actual executable is the compiled version of the source code - a separate file/memory space. While the compilation is running the source code .py file is closed and can be removed.

When the program is compiled and run as a .exe file it will not remove itself because it is still open and running and therefore has a permission access denied error.

It is trying to remove itself because file = sys.argv[0]

(If you want to remove your running exe maybe start another program that does the removing from the exe and exit exe so that it is closed before removal - kind of awkward.)

InhirCode
  • 338
  • 1
  • 4
  • 5