When I am running following code with a on a system not existing /AnewDir
directory then after the code exits there is a new directory on the system:
from subprocess import Popen, PIPE
from os.path import isdir
from time import sleep
from os import system
#v--- (Miracle-SWITCH) NEW LINE / BACKSPACE
#""" Move cursor after the beginning # but before the triple quotes.
#^-- Press Return to switch to execution of the code part removing the
# directory. Then press Backspace to switch back to execution of the
# leading code block (miracle-SWITCH).
sudoShCmd = Popen(["sudo", "-S", "mkdir", "/AnewDir"], stdin=PIPE)
sudoShCmd.stdin.write(b'PUT YOUR ROOT PASSWORD HERE') # <<< ###
sleep(1); print()
system("if [ -x /AnewDir ]; then echo isThere; else echo isNotThere; fi")
print(f'{isdir("/AnewDir")=}')
sleep(3); print()
system("if [ -x /AnewDir ]; then echo isThere; else echo isNotThere; fi")
print(f'{isdir("/AnewDir")=}')
"""
sudoShCmd = Popen(["sudo", "-S", "rmdir", "/AnewDir"], stdin=PIPE)
sudoShCmd.stdin.write(b'PUT YOUR ROOT PASSWORD HERE') # <<< ###
#sleep(1); print()
system("if [ -x /AnewDir ]; then echo isThere; else echo isNotThere; fi")
print(f'{isdir("/AnewDir")=}')
#"""
but the checks for the existence of the directory fail to provide the proper status reporting that the directory isn't there (but you can check from the console of with a File Manager that the directory is there). It doesn't help to pause for 3 seconds in order to get the proper response to the question if the directory exist.
Is there a way to reliably check from within the code creating the directory if the directory was actually created?
Or is the above question only the result of total confusion? Am I missing something in my way of thinking what is happening there?
The answer provided by Constantin Hong shows a way to change the code preceding the checks for the directory existence so that the checks deliver the proper result. This shows that the preliminary code somehow causes the problem, but ... to make these checks work properly isn't necessary to change the code. It would be sufficient to run them from another script after the exit of the script creating/deleting the directory ...
Does the provided answer imply that there is no way to reliably check for existence of a directory in the same code which creates it?