【问题标题】:OSError: [WinError 17] The system cannot move the file to a different disk driveOSError: [WinError 17] 系统无法将文件移动到不同的磁盘驱动器
【发布时间】:2021-11-23 08:52:40
【问题描述】:

我正在尝试将文件从C:/ 驱动器自动移动到D:/ 驱动器,

import shutil
class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        for filename in os.listdir(folder_to_track):
            src = folder_to_track + "/" + filename
            new_destination = folder_destination+ "/" + filename
            #os.rename(src, new_destination)
            shutil.move(src, new_destination)

folder_to_track = '/Users/sia/Downloads/Telegram Desktop'
folder_destination = 'D:/test_folder'
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, folder_to_track, recursive=True)
observer.start()

我遇到了这个错误:

OSError: [WinError 17] The system cannot move the file to a different disk drive: '/Users/sia/Downloads/Telegram Desktop/13940616000112.pdf' -> 'D:/test_folder/13940616000112.pdf'

我该怎么办? 谢谢

【问题讨论】:

  • 您已将@sj95126 的建议更改编辑为您的问题,但您尚未接受他们的回答,或添加任何类型的评论,因此不清楚您的问题的当前状态是什么。建议是否解决了问题,还是保持不变,还是什么?
  • 我发现了我的错误并编辑了问题。谢谢

标签: python


【解决方案1】:

os.rename() 不会跨驱动器移动文件。相反,请尝试使用shutil.move:

import shutil
shutil.move(src, new_destination)

【讨论】:

    猜你喜欢
    • 2014-02-02
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多