![](/img/trans.png)
[英]Cannot find the path specified when attempting to rename certain files in Python
[英]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.