首页 > 解决方案 > Traceback(最近一次调用最后一次):(无法复制树“%s”:不是目录”% src)

问题描述

我想使用 python 编写一个小脚本,该脚本进入我的谷歌驱动器文件夹并将那里的所有文件复制到具有当前日期的文件夹中。所以 fromDirectory 显然是我的程序应该从中复制文件的目录,而 todirectory 是外部硬盘上的目录。我已经检查了路径是否正确,并且我确定它们是正确的。不幸的是,出现了一个错误:

Traceback (most recent call last):
  File "C:\Users\Michael\Desktop\test.py", line 13, in <module>
    copy_tree(fromDirectory, toDirectory)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 124, in copy_tree
    "cannot copy tree '%s': not a directory" % src)
distutils.errors.DistutilsFileError: cannot copy tree 'C:/Users/Michael/Desktop/Google Drive': not a directory

代码:

import time
import sys
import shutil
from distutils.dir_util import copy_tree
import os
from datetime import datetime

today = datetime.now()

fromDirectory = "C:/Users/Micheal/Desktop/Google Drive"
toDirectory = "S:/" + today.strftime('%d%m%Y') 

copy_tree(fromDirectory, toDirectory)

希望得到帮助!

编辑: 我很愚蠢:

我在 PC1 上编写了我的程序,现在在 PC2 上对其进行了测试,所以我没有注意到 PC1 的桌面上确实有一个 Google Drive 文件夹,但在 PC2 的桌面上却没有(只是 Google Drive 文件夹的快捷方式)。对于 PC2,正确的路径是“C:/Users/Michael/Google Drive”。

标签: pythonshutilcopytree

解决方案


问题可能出' '在“Google Drive”中,尝试将其重命名为“Google_Drive”或“Google-Drive”。您也可以尝试替换"C:/Users/Micheal/Desktop/Google Drive""C:/Users/Micheal/Desktop/'Google Drive'",因为这就是它在 bash 脚本中的工作方式,但我不能保证在这种情况下它会工作

编辑:您还可以\在空格前添加一个,因此Google Drive变为Google\ Drive.


推荐阅读