操纵 os.path

Manipulating os.path

我正在尝试检查某个文件是否存在,但是,存在的文件都保存为 .png。有没有办法使用 os.path 查看文件是否存在而没有 .png 部分?例如,我想查看 example.png 是否存在,所以我输入 "example" 而不是 "example.png"

这是我目前的情况:

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: "))

如果文件是 .png,这显然有效,但我该如何更改它,所以我需要做的就是检查没有 .png 的第一位?

您只需将您的输入与“.png”进行字符串连接

这看起来像:

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'