Python IOError:[Errno 2]没有这样的文件或目录–;步行街
我正在尝试运行以下脚本,该脚本只需读取图像并再次保存即可:Python IOError:[Errno 2]没有这样的文件或目录–;步行街,python,file,os.walk,ioerror,Python,File,Os.walk,Ioerror,我正在尝试运行以下脚本,该脚本只需读取图像并再次保存即可: from PIL import Image import os rootdir = '/home/user/Desktop/sample' for subdir, dirs, files in os.walk(rootdir): for file in files: im = Image.open(file) im.save(file) 但是,我得到以下错误: Traceback (most
from PIL import Image
import os
rootdir = '/home/user/Desktop/sample'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
im = Image.open(file)
im.save(file)
但是,我得到以下错误:
Traceback (most recent call last):
File "test.py", line 10, in <module>
im = Image.open(file)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2258, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: '1.jpg'
回溯(最近一次呼叫最后一次):
文件“test.py”,第10行,在
im=图像。打开(文件)
打开文件“/usr/lib/python2.7/dist packages/PIL/Image.py”,第2258行
fp=内置的.open(文件名为“rb”)
IOError:[Errno 2]没有这样的文件或目录:“1.jpg”
因此,我试图做的只是读取文件1.jpg
并再次保存,前提是1.jpg
位于目录中
如何解决此问题
谢谢。您需要提供一个完全限定的路径,因为
文件
只包含尾部,而不是整个路径
您可以使用os.path.join
将根连接到尾部:
for root, dirs, files in os.walk(rootdir):
for file in files:
path = os.path.join(root, file)
im = Image.open(path)
im.save(path)
您需要提供一个完全限定的路径,因为file
只包含尾部,而不是整个路径
您可以使用os.path.join
将根连接到尾部:
for root, dirs, files in os.walk(rootdir):
for file in files:
path = os.path.join(root, file)
im = Image.open(path)
im.save(path)