我试图检查某个文件是否存在,但是,存在的文件被保存为.png。是否有一种方法可以使用os.path来查看没有.png部件的文件是否存在?例如,我想看看example.png是否存在,所以我输入“示例”而不是"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的第一个位?
发布于 2020-03-03 23:06:46
您只需将输入与“.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'https://stackoverflow.com/questions/60516725
复制相似问题