0

Before open this question, I was looking at similar question, but its answer does not work in this case. After I start the process, I want to delete script folder, but I cannot. My code:

from subprocess import Popen

DETACHED_PROCESS = 0x00000008
CREATE_NEW_PROCESS_GROUP = 0x00000200

app = r"C:\Windows\notepad.exe"

Popen(app, creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, close_fds=True)

After running the script, I want to delete script folder, and I get:

enter image description here

I can delete the folder only if I close the process. Do you have any suggestion about how can I start the process without locking script folder?

A. Ilie
  • 71
  • 1
  • 5

1 Answers1

0

Do you mean you want to delete the folder containing the python script itself? That sounds a bit sketchy.

me = os.path.abspath(sys.argv[0])
where = os.path.dirname(me)
args = ['cmd.exe', '/C', 'rd', '/S', '/Q', where]
Popen(args, creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, close_fds=True)
  • Yes, the folder containing the script itself. In our framework, we run the Python tests from a powershell script, and last thing is to clean up (delete the folder). And that step fails, because we start a process from a Python script. – A. Ilie May 22 '20 at 16:01
  • 1
    Have you tried changing the `cwd` when starting your process? I.e., `Popen(..., cwd='somewhereelse')` –  May 22 '20 at 16:06