python - Shutil - DS_store 文件阻塞移动问题

标签 python shutil

尝试处理我的第一个脚本的新 Python 开发人员。目标是编写一个应用程序,通过根据文件类型将文件放入子文件夹来清理我的下载文件夹。

这是我的代码:

import shutil
import os

directory = "/Users/Gustaf/downloads"
file_type_list = []
for filename in os.listdir("/Users/Gustaf/downloads"): #insert your downloads folder path
    path = directory
    file_type = os.path.splitext(filename)[1]
    if file_type not in file_type_list:
        file_type_list.append(file_type)
    if file_type in file_type_list:
        continue
    try:
        print(directory + file_type.replace(".", "/"))
        os.mkdir(directory + file_type.replace(".", "/"))
    except OSError as error:
        print(error)

for filename in os.listdir("/Users/Gustaf/downloads"):
    movable_file_path = directory + "/" + "%s" % (filename)
    file_type = os.path.splitext(filename)[1]
    file_type_no_extension = file_type.replace(".", "")
    file_no_extension = os.path.splitext(filename)[0] #used for the full file path in shutil.move
    fileDestination = directory + "/" + "%s" % (file_type_no_extension)

    if os.path.isdir(movable_file_path) == True:
        #skip directories
        print(movable_file_path)
        print("THIS IS A FOLDER" + "\n")
    
    if os.path.isfile(movable_file_path) == True:
        #The files you actually want to move
        print(filename)
        print("THIS IS A FILE" + "\n")
        shutil.move(movable_file_path, fileDestination) 

第一部分有效,程序为每种文件类型创建文件夹,但是当我尝试移动文件时,我得到了这个:

Traceback (most recent call last):
  File "/Users/Gustaf/Desktop/Programming/downloads_sorter/main.py", line 41, in <module>
    shutil.move(movable_file_path, fileDestination) 
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 786, in move
    raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path '/Users/Gustaf/downloads/.DS_Store' already exists

filenamemovable_file_pathfileDestination 的输出如下:

DS_Store
/Users/Gustaf/downloads/DS_Store
/Users/Gustaf/downloads/
92722314_10157775412048005_7592678894226898944_n.jpg
/Users/Gustaf/downloads/92722314_10157775412048005_7592678894226898944_n.jpg
/Users/Gustaf/downloads/jpg
epub-download-atomic-habits-by-james-clear-9781847941831-fhy.epub
/Users/Gustaf/downloads/epub-download-atomic-habits-by-james-clear-9781847941831-fhy.epub
/Users/Gustaf/downloads/epub

第一个是导致问题的 (DS_store)。有些文件能够移动,但遇到这个后我无处可去。我做错了什么?

最佳答案

对于以下内容:

  • 文件名:DS_Store
  • 可移动文件路径:/Users/Gustaf/downloads/DS_Store
  • 目的地:/Users/Gustaf/downloads/

您的代码所做的是尝试将 DS_Store 从 /Users/Gustaf/downloads/DS_Store/Users/Gustaf/downloads/DS_Store 这实际上是同一个地方。您根本没有尝试移动 DS_Store,因此应该忽略它。您可以通过检查 movable_file_path

中是否存在 fileName 来执行此操作

在这种情况下,您可以按如下方式更改 if 语句:

if os.path.isfile(movable_file_path) and filename not in movable_file_path:
    #The files you actually want to move
    print(filename)
    print("THIS IS A FILE" + "\n")
    shutil.move(movable_file_path, fileDestination) 

关于python - Shutil - DS_store 文件阻塞移动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63940750/

相关文章:

python - 安装 pywin32 时出错(在 Ubuntu 上)

Python字典迭代器性能

python - 如何从 Raw_Input 字符串值创建循环

python - Matplotlib:如果使用关键字 sym,箱线图离群值颜色会发生变化

python - 如何使用 Python 选择特定目录从源复制到新目标?

python - 将文件复制到目录时出现 FileNotFoundError

python - 如何在 Flask 应用程序中重置/清理请求参数

python - 从 zip 中提取某些文件但不是完整的目录

python - 使用 shutil.make_archive() 压缩目录,同时保留目录结构

python - 如何将 .tar.gz 文件转换为shutil.copyfileobj 的类文件对象?