Ok so I have a the name of a file as an raw input. But I the code to only use an already existing file and not and not be able to type in everything and just create new files.
So how can I make the program only use an already existing file and NOT create a new one if the input is wrong?
lista = {"police":"911"}
functiontext = raw_input("call function ")
arguments = raw_input("input file name ")
def save(lista,arguments):
filen = arguments
spara = lista
fil = open(filen + ".txt","w")
for keys, values in spara.items():
spara_content = keys + ": " + values + "\n"
fil.write(spara_content)
fil.close()
Consider using os.path.exists() or os.path.isfile() to test if the file is there. For example:
def save(lista,arguments):
filen = arguments
spara = lista
fname = filen + ".txt"
if os.path.isfile(fname):
fil = open(filen + ".txt","w")
else:
print("The file {} does not exist, skipping".format(fname))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.