首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >操纵os.path

操纵os.path
EN

Stack Overflow用户
提问于 2020-03-03 22:55:51
回答 1查看 41关注 0票数 0

我试图检查某个文件是否存在,但是,存在的文件被保存为.png。是否有一种方法可以使用os.path来查看没有.png部件的文件是否存在?例如,我想看看example.png是否存在,所以我输入“示例”而不是"example.png“

以下是我到目前为止所拥有的:

代码语言:javascript
复制
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的第一个位?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-03 23:06:46

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

这看起来应该是:

代码语言:javascript
复制
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'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60516725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档