I'm trying to check to see if a certain file exists, however, the files that exist are saved as .png. Is there a way to use os.path to see if the file exists without the .png part? e.g I want to see if example.png exists so I enter "example" instead of "example.png"
Heres what I have so far:
import os
filepath = str(input("If you want to make a new file, type None otherwise enter the file that it is being written to: "))
while os.path.isfile(filepath) is False:
if filepath == "None":
filepath = functnone()
break
print("That filepath doesn't exist")
filepath = str(input("If you want to make a new file, type None otherwise enter the file that it is being written to: "))
This obviously works if the file is .png but how can I change it so all I would need to do is check the first bit without the .png?
asked Mar 3, 2020 at 22:55
LordLightingXII
511 silver badge6 bronze badges
-
1Just change your filepath lines, like: filepath = str(input("If you want to make a new file, type None otherwise enter the file that it is being written to: "))+'.png'maurera– maurera2020年03月03日 22:58:33 +00:00Commented Mar 3, 2020 at 22:58
-
Oh I'm an idiot I forgot you could concatenate it in, thank youLordLightingXII– LordLightingXII2020年03月03日 23:00:14 +00:00Commented Mar 3, 2020 at 23:00
1 Answer 1
You just need to do a string concatenation of your input with '.png'
This would look something like:
import os
filepath = str(input("If you want to make a new file, type None otherwise enter the file that it is being written to: "))+'.png'
while os.path.isfile(filepath) is False:
if filepath == "None":
filepath = functnone()
break
print("That filepath doesn't exist")
filepath = str(input("If you want to make a new file, type None otherwise enter the file that it is being written to: "))+'.png'
answered Mar 3, 2020 at 23:06
maurera
1,6893 gold badges18 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py