簡體   English   中英

如果 Pillow 不存在,則在該目錄中創建目錄

[英]Create directory in Pillow if it doesn't exist

我有一個腳本,可以根據特定的用戶 ID 獲取圖像並將它們保存到目錄中。 這些目錄尚不存在。 例如:

媒體/1/ex.jpg 媒體/2/ex.jpg 。 . .

當我運行我的腳本時,我收到以下錯誤:

FileNotFoundError: [Errno 2] No such file or directory:

但是,當我創建一個目錄(例如 (1))並運行它時,它會將圖像保存在 1 目錄中。 如果目錄不存在,有沒有辦法讓 Pillow 能夠創建目錄?

你可以用基本的python來做。 此示例假定 'media' 已存在,並且它位於運行腳本的同一目錄中。

from os import mkdir, path, getcwd

def get_subdirectory(sd):
    dir = path.join(getcwd(), f'media/{sd}')
    if not path.isdir(dir):
        mkdir(dir)
    return dir
    
#image save example
#image.save(f"{get_subdirectory('1')}/ex.jpg")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM