繁体   English   中英

检查文件是否存在带有python变量的文件

[英]checking if a file exists with a variable on python

我在尝试检查文件是否存在时遇到问题。 问题来了,文件名必须由用户定义,因为它的名称是“ Autentication system”,问题是无论我输入什么密码,总会发回该文件名不存在。

contador = 1 
def B():
    pregunta = int(input("Introduce la clave: "))
    if os.path.isfile(pregunta)== True:
       os.system("cls")
       print("Contraseña Correcta")
       seleccion()
    else:
        print("Contraseña Incorrecta")
        global contador
        contador +=1
        if contador <= 3:
           B()
        else:
             print("Se ha excedido el numero maximo de intentos")
             exit()
def C():
    pregunta2 = int(input("cambiar la clave o iniciar windows?"))
    if pregunta2 == ("cambiar la clave"):
       nuevaclave = int(input("Cual sera la nueva clave?"))
       archi=open("nuevaclave","w")
       archi.close()
       os.system("Attrib +H nuevaclave")
       os.remove(Pregunta)
       print("Se procedera al inicio del sistema")
       os.system("start explorer.exe")
       exit()
    elif pregunta2 == ("iniciar windows"):
         print("Se procedera al inicio del sistema")
         os.system("start explorer.exe")
         exit()
    else:
        print("No te entiendo")
        C()
print("Bienvenido al sistema de autenticacion")
import time
time.sleep(5)
import os
import os.path
os.chdir("D:\Marcos\Desktop")
if os.system("mkdir Clave") == 0:
   os.system("mkdir Clave")
   os.system("Attrib +H Clave")
   os.chdir("Clave")
   archi=open("12345","w")
   archi.close()
   os.system("cls")
   print("Se ha designado la clave predeterminada la cual es 12345")
 else:
     pass

time.sleep(5)
os.system("taskkill /f /im explorer.exe")
B()

我在这部分上遇到了麻烦

def B():
       pregunta = int(input("Introduce la clave: "))
       if os.path.isfile(pregunta)== True:
          os.system("cls")
          print("Contraseña Correcta")
          seleccion()
       else:
           print("Contraseña Incorrecta")
           global contador
           contador +=1
           if contador <= 3:
              B()
           else:
               print("Se ha excedido el numero maximo de intentos")
               exit()

这可能是一个新手问题,但是我已经在两天前开始学习了,我无法解决这个问题。

是的, os.path.isfile(integer)将始终为False 您总是将输入转换为整数。 你可能不应该。 尝试使用pregunta=raw_input(...)做同样的事情

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM