使用[Errno2]重命名脚本没有这样的文件或目录 [英] Rename script with [Errno2] No such file or directory

查看:85
本文介绍了使用[Errno2]重命名脚本没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在外置驱动器上有一个文件夹,其中包含超过4万张图像,用作间隔拍摄的序列.在使用ffmpeg之前,我需要在所有文件中添加尾随零.我对解决方案的尝试如下所示:

I have a folder with over 40 thousand images on an external drive, to use as a sequence for a timelapse. Before using ffmpeg though, I need to add trailing zeros to all the files. My attempt at a solution is shown below:

import os

path = '/Volumes/Arquivo\ \(carlosbgois@gmail.com\)/stadium_billiard/video/'
for filename in os.listdir(path):
    num = filename[:-4]
    num = num.zfill(4)
    new_filename = num + ".png"
    os.rename(os.path.join(path, filename), os.path.join(path, new_filename))

运行时,在第5行出现错误 [Errno2]没有这样的文件或目录.这些文件分别命名为0.png,1.png,...,32220.png,等等.关于什么可能导致此的任何想法?

When running, I get the error [Errno2] No such file or directory on line 5. The files are named as 0.png, 1.png, ..., 32220.png, and so on. Any ideas on what may be causing this?

祝你有美好的一天(:

推荐答案

指出,当路径以字符串形式给出时,不需要在末尾加反斜杠和特殊字符,因为它们在终端中.因此,工作代码为

Figured out that when the path is given as a string, the backslashes preceding spaces and special characters are not needed, as they are in the terminal. Hence the working code is

import os

path = '/Volumes/Arquivo (carlosbgois@gmail.com)/stadium_billiard/video/'

for filename in os.listdir(path):
    num = filename[:-4]
    num = num.zfill(5)
    new_filename = num + ".png"
    os.rename(os.path.join(path, filename), os.path.join(path, new_filename))

谢谢!

这篇关于使用[Errno2]重命名脚本没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆