将 matplotlib savefig .png 传输到 dest 文件夹

Transfer matplotlib savefig .png to dest folder

我总是收到错误消息:

"raise Error("无法将目录 '%s' 移动到自身中" shutil.Error: 无法将目录“/Users/mattbecute/Desktop/Valuable Projects/Webscrapingprojects/CovidMonitor/Covid 饼图”移动到其自身“/Users/mattbecute/Desktop/Valuable Projects/Webscrapingprojects/CovidMonitor/Covid 饼图 Charts/Worldwide/November/”中。“

这是代码:

def transferPhoto(country):
    source = "/Users/mattbecute/Desktop/Valuable Projects/Webscrapingprojects/CovidMonitor/"
    destination = "/Users/mattbecute/Desktop/Valuable Projects/Webscrapingprojects/CovidMonitor/Covid Pie Charts/" + country.capitalize() + "/November/" 
    files = os.listdir(source)
    for f in files:
            if os.path.splitext(f)[1] in ('.png'):
                    shutil.move(source + f, destination)

此功能是将当前文件夹中保存的matplotlib饼图(.png)转为国家文件名,转为当前月份,即11月。

嗯,你的 if-case 设计得不是很好;检查如果你 运行 这两行会发生什么:

if "" in ".png":
    print("yep")

所以要么你把你的 if case 改成更健壮的东西,比如

if os.path.splitext(f)[1] == '.png':

或者至少改变比较:

if ".png" in os.path.splitext(f)[1]: