Need Help with checking if a file is open!

Hello! I'm very new on Python. I've been studying it for a couple of months, so I decided to do a mini project/game for my boyfriend.

It basically consists in step by step minigames (quiz game, rock paper scissors, etc) that you "unlock" once completed the last minigame.
Thing is, I also wanted to implement the start of the game once a specific created .txt file is opened.
I've read that Python cannot see if the file is opened in another process, but also read about other workarounds.

I tried different kind of codes; they all work but all the time, not only when the .txt is open.
These are my codes: can someone help me?

The program starts like this:

import os

folder1 = "Benvenuto! Clicca pure!" directorypath1 = os.path.join("C:\Users\hay\Desktop", folder1) os.makedirs(directorypath1, exist_ok=True)

path = "C:\Users\hay\Desktop\Benvenuto! Clicca pure!"

os.makedirs(path, exist_ok=True)

testo1 = os.path.join(path, "Istruzioni.txt")

with open(testo1, 'w') as file: file.write("Quindi, eccoci qui. Tutto ok? Questo è il file di istruzioni. \nResterà in questa cartella, ma puoi spostarlo dove più ti aggrada.\n\n") file.write("Innanzitutto, ti chiedo di essere clemente con me per eventuali errori. Dovrebbe essere tutto a posto, ma non si sa mai.\n") file.write("Spiegazione delle prove.\n") file.write("Finale su anniversario. Blabla Clicca su")

path1 = "C:\Users\hay\Desktop\Benvenuto! Clicca pure!\Istruzioni.txt"

Then, these are the things I tried:

def eaperto(path1):
try:
  
    file = open(path1, 'r')
    file.close()
    return False
except IOError:
  
    return True

import os

if os.path.exists(path1): try: os.rename(path1, path1) print('Access on file "' + path1 +'" is available!') except OSError as e: print ('Access-error on file "' + path1 + '"! \n' + str(e))

while True: if eaperto(path1): print("File is currently open.")

    nome = input("What's your name? ")
    if nome.lower() == "john" or nome.lower() == "jo":
        print("Let's go")
        break
    elif nome.lower() in [" ", ""]:
        print("Idiot")
    else:
        print("You are not who I expected")
        break
else:
    print("File is not open.")

N.2:

with open(path1) as file:
Statement = True and  file.read()

with True: nome = input( "What's your name?") if nome.lower() == "john" or nome.lower() == "jo": print("Let's go") elif nome.lower() in [" ", ""]: print("Idiot") else: print("You are not who I expected")

N.3:

with open(path1) as file: try: file.read() #I also tried here while file1aperto(path1): nome = input("What's your name? ") if nome.lower() == "john" or nome.lower() == "jo": print("Let's go") break elif nome.lower() in [" ", ""]: print("Idiot") else: print("You are not who I expected") break except file.closed() print("Waiting for the file to be closed.")

N.4:
I also tried this but same: wouldn't work only with the file is opened or closed:

def isFileInUse(path1):
try:
    os.rename(path1, path1)
        nome = input("What's your name? ")
    if nome.lower() == "john" or nome.lower() == "jo":
        print("Let's go")
        break
    elif nome.lower() in [" ", ""]:
        print("Idiot")
    else:
        print("You are not who I expected")
        break
    return False
except FileNotFoundError:
    return True

​

Posted Jan 15, 2024, 1:27 PM UTC

22
Loading comments…