0

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
2
  • 1
    Just 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' Commented Mar 3, 2020 at 22:58
  • Oh I'm an idiot I forgot you could concatenate it in, thank you Commented Mar 3, 2020 at 23:00

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.