1

我正在尝试制作一个程序来组织我的下载文件夹,每次我下载一些东西,但是如果我使用这个代码:

import shutil

shutil.move("/Users/plapl/downloads/.zip", "/Users/plapl/Desktop/Shortcuts/winrar files")
shutil.move("/Users/plapl/downloads/.png", "/Users/plapl/Desktop/Shortcuts/images")

它搜索名为 .zip 和 .png 的文件名,但我希望它搜索该类型的所有文件。谁能告诉我该怎么做?

4

3 回答 3

0

您想遍历目录中的文件。这是来自源代码的示例

import shutil
import os
source = os.listdir("/Users/plapl/downloads/")
destination = "/Users/plapl/Desktop/Shortcuts/winrar files"
for files in source:
    if files.endswith(".zip"):
        shutil.move(files,destination)
于 2020-04-22T14:12:40.853 回答
0

我以此为基础做了一些事情,但它说未解决的参考“文件”

import shutil

import os


source = os.listdir("/Users/plapl/Downloads/")

destination1 = "/Users/plapl/desktop/Shortcuts/images"

destination2 = "/Users/plapl/Shortcuts/winrar files"

destination3 = "/Users/plapl/torrents"

for files in source:
    if file.endswith(".png"):
        shutil.move(files, destination1)

    if file.endswith(".zip"):
        shutil.move(files, destination2)

    if file.endswith(".torrent"):
        shutil.move(files, destination3)

于 2020-04-22T14:52:26.337 回答
0

它抱怨file未定义的变量名。

您应该使用files,因为那是您的迭代变量名称。

于 2020-04-22T15:09:27.020 回答