python - 文件未找到错误: [Errno 2] No such file or directory: '/tmp/cats-v-dog

标签 python

我使用 os.mkdir 创建一些目录

try:
#YOUR CODE GOES HERE
os.mkdir("/tmp/cats-v-dogs/")
os.mkdir("/tmp/cats-v-dogs/training/cats")
os.mkdir('/tmp/cats-v-dogs/training/dogs/')
os.mkdir('/tmp/cats-v-dogs/testing/cats')
os.mkdir('/tmp/cats-v-dogs/testing/dogs')
except OSError:
pass

我有 list : 训练=[狗1,狗2,...,狗100] TRAINING_DOGS_DIR = "/tmp/cats-v-dogs/training/cats/" 我想使用 trainig 元素将文件从一个目录复制到另一个目录:

for list1 in training:
copyfile(os.path.join(SOURCE,list1),TRAINING_DOGS_DIR)

运行该循环后,这是我得到的错误:

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/cats-v-dogs/training/dogs/'

一些帮助或建议,谢谢/

最佳答案

try- except block 是这里伤害你的地方。 os.mkdir 只能在深度上一次创建一个目录,因此第二行到第五行都失败,但因为您捕获了异常,所以错误是正确的。 p>

解决方案:将所有 os.mkdir 替换为 os.makedirs,这将毫无问题地创建所有这些。并摆脱try- except!仅当您确切知道要捕获的内容时才应使用这些。

关于python - 文件未找到错误: [Errno 2] No such file or directory: '/tmp/cats-v-dog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64595729/

相关文章:

Python list of lists true 中的所有项目

python - Flask request.form 包含数据,但 request.data 为空且 request.get_json() 返回错误

python - 获取最近几天创建的对象列表(按天分组)

python - 迭代多维数组并跟踪/执行迭代索引操作的最佳方法是什么

python - AttributeError : partially initialized module 'turtle' has no attribute 'Turtle' (most likely due to a circular import)

python - 使用 db_manager 时,“QuerySet”对象没有属性 'login_name'

python - 用列表中的值替换字符串中的变量名并避免冗余替换

python - Elasticsearch 的upsert函数?

python - 将 Count 连接到 pandas 中的原始 DataFrame

python - 将 python 中的数据帧转换为以下格式的 json