Манипулирование os.path - PullRequest
0 голосов
/ 04 марта 2020

Я пытаюсь проверить, существует ли определенный файл, однако существующие файлы сохраняются как .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?

1 Ответ

1 голос
/ 04 марта 2020

Вам просто нужно выполнить конкатенацию ввода с помощью '.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'
...