'FileNotFoundError: [Errno 2] No such file or directory: Discrepancy between programs

at the moment I'm writing several tests with Selenium and I use Python. For rudimentary logging i made this function in order to output all prints to a file:

# Datengenerierung für Logname, Kundennummer, Zeit
num_dt = datetime.now()
num_dt_l = num_dt.strftime("%d_%m_%Y %HH_%MM_%SS")
rndnr = str(random.randint(1, 9999))
log_n = r"Logs/" + str(num_dt_l) + "__" + rndnr + r".txt"


# Print-Funktion
def logp(lv1, lv2):
    with redirect_stdout(open(log_n, 'a')):
        print("-----------\n" + str(datetime.now().strftime("%HH:%MM:%SS")) + " " + str(lv1) + " " + str(lv2) + ".")


with redirect_stdout(open(log_n, 'a')):
    print("****************************\nTest", "started\n****************************")

logp("Global Number: " + str(rndnr), "")
logp("Datum ist:", num_dt_l)

And in one of two programs it works perfectly fine, however, in another program in the same directory with the very same code it throws following error:

Traceback (most recent call last): File "Z:\testing\dom\Testing Projects\Project1\chrome_beratung.py", line 37, in with redirect_stdout(open(log_n, 'a')): FileNotFoundError: [Errno 2] No such file or directory: 'Logs/18_02_2022 12H_44M_20S__2019.txt'

I don't understand how the same code, with the same directory and path would work for one program and not for the other. Can somebody explain how this happens? Does anybody know a solution? Have I overlooked something?

Greetings, Knolfuns



Solution 1:[1]

you can try to change the file address (log_n variable) to an absolute address and run the app as root to avoid privilege issues

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alejandro Vistorte