簡體   English   中英

重命名路徑中帶有空格的文件

[英]Rename files with spaces in path

我有一個名為“ True Detective”的文件夾

import shutil
file = "true.detective.s01e04.720p.hdtv.x264-killers.mkv"
dest = "/home/sharefolder/things/Videos/Series/True Detective/"
shutil.copy(file, dest)

它說:

IOError: [Errno 21] Is a directory: '/home/sharefolder/things/Videos/Series/True Detective/'

文件夾 “/家/ sharefolder /事/視頻/系列/真偵探/” 的存在。

當我設置一個沒有空格的文件夾時,一切正常。 有任何想法嗎?

目標目錄必須存在, shutil.copy()才能工作; os.path.isdir(dest)必須為True 如果dest不存在, shutil最終將嘗試將源文件名復制到目錄名稱(包括結尾的/ ),這就是引發異常的原因。

您可以調用os.makedirs()以確保首先正確創建了目標目錄:

進口shutil進口操作系統

file = "true.detective.s01e04.720p.hdtv.x264-killers.mkv"
dest = "/home/sharefolder/things/Videos/Series/True Detective/"
try:
    os.makedirs(dest)
except OSError:
    # destination directory already exists
    pass
shutil.copy(file, dest)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM