关于python中的 “ FileNotFoundError: [Errno 2] No such file or directory: '……'问题 ”
今天在学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: '……'问题 ”的更多相关文章
- Python中的用open打开文件错误,FileNotFoundError: [Errno 2] No such file or directory:
学习python的文件处理,刚开始打开文件,代码如下 f = open('test_file.txt', 'r', encoding='UTF-8')RES = f.readlines()print( ...
- python文件操作的坑( FileNotFoundError: [Errno 2] No such file or directory...)
环境:Windows8.1, Python3.6 pycharm community 2017 c盘下有一个配置文件:setup with open('c:\\setup','r') as ...
- 关于JPype报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误的解决
部署到线上的项目正常运行一年,今天早上突然报FileNotFoundError: [Errno 2] No such file or directory: '/usr/lib/jvm'错误. JPyp ...
- FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题解决
FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题 最近在学习爬虫,想爬一些图片并保存到本地,但是在下载图片 ...
- 对于python中“FileNotFoundError: [Errno 2] No such file or directory”的解决办法
在我们使用vscode运行Python代码时遇到的情况 一.出现原因:这里是由于Vscode中,python里的路径是相对与工作目录来进行定位的.所以在多级目录情况下,若不设置绝对路径,往往找不到相应 ...
- FileNotFoundError: [Errno 2] No such file or directory的解决方法
1.获取当前文件所在路径 basedir = os.path.dirname(__file__) print("basedir:" + basedir) 2.将路径进行拼接 upl ...
- python3 使用selenium +webdriver打开chrome失败,报错:FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'
提示chrome driver没有放置在正确的路径下 解决方法: 1.chromedriver与chrome各版本及下载地址 驱动的下载地址如下: http://chromedriver.storag ...
- 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中左 ...
- Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt
最近安装python,已经安装好,cmd终端中输入python.pip等命令都有用 然而在配置requirements.txt文件过程中,执行语句 “pip install -r requiremen ...
随机推荐
- Yosemite安装libv8和therubyracer
yosemite ruby version升级的时候,会碰到类似 Make sure that `gem install libv8 -v '3.16.14.3'` succeeds before b ...
- MFC CFile类读写文件详解
CFile类提供了对文件进行打开,关闭,读,写,删除,重命名以及获取文件信息等文件操作的基本功能,足以处理任意类型的文件操作. 一个读写文件的例子: 文件I/O 虽然使用CArchive类内建的序列化 ...
- UVALive - 7269 I - Snake Carpet
思路: 多画画就发现从五的时候可以这么填: 六的时候这么填: 七的时候这么填: 看出规律了吗? 没看出的话再画画把. #include <bits/stdc++.h> using name ...
- iOS 建立项目过滤机制 —— 给工程添加忽略文件.gitignore
目前iOS 项目 主要忽略 临时文件.配置文件.或者生成文件等,在不同开发端这些文件会大有不同,如果 git add .把这些文件都push到远程, 就会造成不同开发端频繁改动和提交的问题. ...
- 【Redis学习之二】Redis:redis.conf 配置详解
参数说明redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no2. 当Redis以守护进程方式运行 ...
- 自己封装的ajax
/** * ITCAST WEB * Created by lsy on 2016/5/24. */ /* * 1. 请求的类型 type get post * 2. 请求地址 url * 3. 是异 ...
- centos 安装 jdk8和comcat8
1.去官网下载Linux版本的jdk8 2.进入ags,把tar.gz的压缩包拷贝到用户下指定目录java,然后解压 mkdir /home/ags/java tar -zxv -f jdk-8u11 ...
- centos 安装sftp服务
打开命令终端窗口,按以下步骤操作. 0.查看openssh的版本 ssh -V 使用ssh -V 命令来查看openssh的版本,版本必须大于4.8p1,低于的这个版本需要升级. 1.创建sftp组 ...
- python+requests接口自动化测试
转自https://my.oschina.net/u/3041656/blog/820023 原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测 ...
- CentOS安装mysql并配置远程访问
最近上班挺无聊,每天就是不停的重启重启重启,然后抓log.于是有事儿没事儿的看卡闲书,搞搞其他事情. 但是,公司笔记本装太多乱其八糟的东西也还是不太好. 于是,想到了我那个当VPN server的VP ...