我需要将多个JPG文件中的图像标题信息提取到文本或日志文件中,但是当我运行下面的代码时,我收到一个错误:
for root, dirs, filenames in os.walk(topdir):
for f in filenames:
print(topdir)
print(f)
log = open(topdir + f, 'r')
data = p.get_json(log)
formatted_data =(( json.dumps(data, sort_keys=True,indent=4, separators=(',', ':')) ))
print(data)
print ("There are " + str(len(header_dict)) + " items on the menu.")
运行时出现以下错误:
C:/Users/richie/Desktop/work/imagej/test images and files/XX1
image_D2016-02-03T15-27-56-763207Z_4.jpg
Traceback (most recent call last):
File "C:\Users\richie\Desktop\work\header_dir.py", line 25, in <module>
log = open(topdir + f, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/richie/Desktop/work/imagej/test images and files/XX1image_D2016-02- 03T15-27-56-763207Z_4.jpg'
如何打开图像文件以允许for循环中的函数对其运行?
答案 0 :(得分:1)
您的问题在于此代码;
topdir + f
首先,您应该在路径上使用join
,而不是+
。后者不会在路径和文件之间插入分隔符。
其次,您应该使用root
加入文件名,而不是topdir
。
for root, dirs, files in os.walk(topdir):
paths = [os.path.join(root, f) for f in files]
for p in paths:
log = open(p)
# et cetera
答案 1 :(得分:0)
工作代码:
urlpatterns = [
url(r'^(?P<slug>[-\w]+)/$', Display.as_view(), name="get_post"),
]