zoukankan      html  css  js  c++  java
  • 关于python中的 “ FileNotFoundError: [Errno 2] No such file or directory: '……'问题 ”

    今天在学python时,在模仿一个为图片加上图标并移动到指定文件夹的程序时遇到“FileNotFoundError: [Errno 2] No such file or directory: '1528712892362.jpg'”这个问题。

    讲讲解决方案,因为我要处理的十几张图片是位于 ‘F:图片’ 这个路径下的,但是读取到具体的某一个文件时忘记使用绝对路径,如果没有为文件加上前面的绝对路径的话,我的理解是默认在程序所在的路径作为当前路径来查找的,所以就会报错说找不到文件。

    后来加上代码中的第10行,并在16行中稍作修改就行了。

     1 import os
     2 from PIL import Image
     3 SQUARE_FIT_SIZE=500
     4 LOGO_FILENAME='catlogo.png'
     5 logoIm=Image.open(LOGO_FILENAME)
     6 width1,height1=logoIm.size
     7 logoIm1=logoIm.resize((int(width1/5),int(height1/5)))
     8 logoWidth,logoHeight=logoIm1.size
     9 logoIm=logoIm1
    10 root='F:/图片/'
    11 os.makedirs('withLogo',exist_ok=True)
    12 for filename in os.listdir('F:图片'):
    13     print(filename)
    14     if not (filename.endswith('.png') or filename.endswith('.jpg')) or filename==LOGO_FILENAME:
    15         continue
    16     im=Image.open(root+filename)
    17     width,height=im.size
    18     print('Resizing %s...'%(filename))
    19     im=im.resize((width,height))
    20     print('Adding logo to %s...'%(filename)) 
    21     im.paste(logoIm,(width-logoWidth,height-logoHeight),logoIm)
    22     im.save(os.path.join('withLogo',filename))
  • 相关阅读:
    常见的网络设备:集线器 hub、网桥、交换机 switch、路由器 router、网关 gateway
    Linux 路由表详解及 route 命令详解
    Flannel
    Flannel
    Hugo
    Nginx 实现全站 HTTPS(基于 Let's Encrypt 的免费通配符证书)
    Nginx 安装
    ETCD 简介及基本用法
    Vagrant 手册之 Multi-machine 多机器
    Vagrant 手册之 Provisioning
  • 原文地址:https://www.cnblogs.com/Guhongying/p/9787403.html
Copyright © 2011-2022 走看看