繁体   English   中英

操作 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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM