LordLightingXII
LordLightingXII

Reputation: 51

Manipulating os.path

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?

Upvotes: 0

Views: 91

Answers (1)

maurera
maurera

Reputation: 1679

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'

Upvotes: 1

Related Questions