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

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

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

 import os
from PIL import Image
SQUARE_FIT_SIZE=500
LOGO_FILENAME='catlogo.png'
logoIm=Image.open(LOGO_FILENAME)
width1,height1=logoIm.size
logoIm1=logoIm.resize((int(width1/5),int(height1/5)))
logoWidth,logoHeight=logoIm1.size
logoIm=logoIm1
root='F:/图片/'
os.makedirs('withLogo',exist_ok=True)
for filename in os.listdir('F:\图片'):
print(filename)
if not (filename.endswith('.png') or filename.endswith('.jpg')) or filename==LOGO_FILENAME:
continue
im=Image.open(root+filename)
width,height=im.size
print('Resizing %s...'%(filename))
im=im.resize((width,height))
print('Adding logo to %s...'%(filename))
im.paste(logoIm,(width-logoWidth,height-logoHeight),logoIm)
im.save(os.path.join('withLogo',filename))

关于python中的 “ FileNotFoundError: [Errno 2] No such file or directory: '……'问题 ”的更多相关文章

  1. Python中的用open打开文件错误,FileNotFoundError: [Errno 2] No such file or directory:

    学习python的文件处理,刚开始打开文件,代码如下 f = open('test_file.txt', 'r', encoding='UTF-8')RES = f.readlines()print( ...

  2. python文件操作的坑( FileNotFoundError: [Errno 2] No such file or directory...)

    环境:Windows8.1, Python3.6  pycharm community 2017   c盘下有一个配置文件:setup   with open('c:\\setup','r') as ...

  3. 关于JPype报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误的解决

    部署到线上的项目正常运行一年,今天早上突然报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误. JPyp ...

  4. FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题解决

    FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题 最近在学习爬虫,想爬一些图片并保存到本地,但是在下载图片 ...

  5. 对于python中“FileNotFoundError: [Errno 2] No such file or directory”的解决办法

    在我们使用vscode运行Python代码时遇到的情况 一.出现原因:这里是由于Vscode中,python里的路径是相对与工作目录来进行定位的.所以在多级目录情况下,若不设置绝对路径,往往找不到相应 ...

  6. FileNotFoundError: [Errno 2] No such file or directory的解决方法

    1.获取当前文件所在路径 basedir = os.path.dirname(__file__) print("basedir:" + basedir) 2.将路径进行拼接 upl ...

  7. python3 使用selenium +webdriver打开chrome失败,报错:FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

    提示chrome driver没有放置在正确的路径下 解决方法: 1.chromedriver与chrome各版本及下载地址 驱动的下载地址如下: http://chromedriver.storag ...

  8. VSCode python 遇到的问题:vscode can't open file '<unprintable file name>': [Errno 2] No such file or directory

    代码很简单,就两行: import pandas as pd import netCDF4 as nc dataset = nc.Dataset('20150101.nc') 环境:在VSCode中左 ...

  9. Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt

    最近安装python,已经安装好,cmd终端中输入python.pip等命令都有用 然而在配置requirements.txt文件过程中,执行语句 “pip install -r requiremen ...

随机推荐

  1. Java-单向链表算法

    /** * 数据结构之链表(单向链表) * @author Administrator * */ public class LinkNodeTest { public static void main ...

  2. 将Mongo装为Windows的服务

    PHP的mongo扩展: 首先 下载一个PHP的mongo扩展, 地址:http://download.csdn.net/detail/qq_36387589/9819259 然后修改php.ini. ...

  3. Javascript-逻辑判断或(&&)练习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. strus2 框架介绍

    strus2 执行过程: ActionMapper会去找ActionMapping查找URL请求的映射 1:ActionMapping这个类用name+namespace确定请求的映射, (但是仍然不 ...

  5. Java中如何优雅地删除List中的元素

    在工作中的许多场景下,我们都会使用到List这个数据结构,那么同样的有很多场景下需要删除List中的某一个元素或某几个元素,那么我们该如何正确无误地删除List中的元素的,今天我来教大家三种方式. 前 ...

  6. 论文笔记:语音情感识别(四)语音特征之声谱图,log梅尔谱,MFCC,deltas

    一:原始信号 从音频文件中读取出来的原始语音信号通常称为raw waveform,是一个一维数组,长度是由音频长度和采样率决定,比如采样率Fs为16KHz,表示一秒钟内采样16000个点,这个时候如果 ...

  7. 【翻唱】白金disco

    http://video.yingtu.co/0/bbea91f8-e1ce-4211-9519-2371745f4ec0.mp4 [翻唱]白金disco

  8. ES6学习--Array.from()方法

    Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组. 那么什么是类数组对象呢?所谓类数组对象,最基本的要求就是具有length属性的对象. 1.将类数组对象转换为真正数 ...

  9. SQL学习之简单增删改查

    SQL最常用的语句,就是增删改查: 增删改查的对象,分别是库(文件夹),表(文件),表的内容(表的记录): 一.创建一个基本的表 #create table Student_Info (Name VA ...

  10. 怎么说, 开发会很乐意去主动修改bug?

    怎么说, 开发会很乐意去主动修改bug? 一图顶上千言万语,如下: