关于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))

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

原文地址:https://www.cnblogs.com/Guhongying/p/9787403.html

时间: 2024-08-28 08:07:13

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

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

环境:Windows8.1, Python3.6  pycharm community 2017 c盘下有一个配置文件:setup 1 with open('c:\\setup','r') as f: 2 f.read() 提示一直很诡异:FileNotFoundError: [Errno 2] No such file or directory: 'c:\\setup' 网上有大量关于Python编码错误导致无法打开文件的解决方法,utf8 gbk2312,尝试过一遍全部无效 最后原因很乌龙:

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

1.获取当前文件所在路径 basedir = os.path.dirname(__file__) print("basedir:" + basedir) 2.将路径进行拼接 upload_path = os.path.join(basedir, "static/upload", filename) 原文地址:https://www.cnblogs.com/zengsf/p/9882824.html

使用py2exe 将python文件转成exe文件(以及 error: [Errno 2] No such file or directory: 'MSVCP90.dll'解决方法)

用python写程序真的很简单,当我们开发出功能后,特别是带窗体的python程序,需要将python程序转成双击运行的exe程序.我们需要一个第三方模块 py2exe,就是将py转成exe的模块.只需py2exe怎么安装,我这里就不详细说明了,直接到官网上下载对应python的py2exe版本,然后是傻瓜式安装,并且安装好后py2exe模块会自动添加到python的路径中. 然后就简单了,我们在要被转换成exe的py文件目录下新建一个 setup.py 文件,将代码贴进去: #coding=u

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

最近安装python,已经安装好,cmd终端中输入python.pip等命令都有用 然而在配置requirements.txt文件过程中,执行语句 “pip install -r requirement.txt” 时报错 “Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'”, 经过一番折腾,查出原因是在执行这一语句前需要先创建requirements.txt 文件,所

ansible执行shell模块和command模块报错| FAILED | rc=127 >> /bin/sh: lsof: command not found和| rc=2 >> [Errno 2] No such file or directory

命令: ansible -i hosts_20 st  -m shell -a 'service zabbix_agentd star'  -K --become ansible -i hosts_20 st  -m shell -a 'lsof -i:10050'  -K --become 在shell模块报错:| FAILED | rc=127 >>/bin/sh: lsof: command not found 在command模块报错:| rc=2 >>[Errno 2]

[Errno 2] No such file or directory

Centos7.5 执行ansible命令报错 问题: [root@m01 ~]# ansible servers -a "hostname|grep web" -i ./hosts 172.16.1.7 | FAILED | rc=2 >> [Errno 2] No such file or directory 原因: 默认使用的是command模块,command模块不能使用管道 解决方法: 使用shell模块执行命令 [root@m01 ~]# ansible ser

python 中socket.error: [Errno 10022]

在用python中的socket模块进行udp变成时 , 一直出现这个错误 下面先把错误代码贴出来 server: import socket import random,time s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM) host = '127.0.0.1' port = 21567 addr = (host , port) if __name__ == '__main__': s.bind(addr) while True:

python smbus IOError: [Errno 2] No such file or directory

1.打开配置文件 sudo nano /boot/config.txt 打开以下选项 "dtparam=i2c_arm=on" ctrl + o 保存 ctrl + x 退出 2.刷新配置文件 sudo raspi-config 3.打开 I2c选项 4.重启 5.再次运行 参考: https://www.raspberrypi.org/forums/viewtopic.php?t=100189 原文地址:https://www.cnblogs.com/sea-stream/p/111

OSError: [Errno 2] No such file or directory

执行Python脚本时,报如下错误: ['mkdir -p /tmp/gluster_volume_files.can.not.delete'] [E 150529 11:36:14 web:1030] Uncaught exception GET /api/v1/volume/df/tank (10.58.164.150) HTTPRequest(protocol='http', host='10.160.140.23:8000', method='GET', uri='/api/v1/vol