尝试打开现有文件时出现 IOError [英] IOError when trying to open existing files
问题描述
我编写的用于从特殊文本文件中提取一些信息的 Python 程序有一个小问题.循环(下面的代码)需要执行我的函数 extract_zcoords()
超过 500 个文件(1 个文件给出一个列表),以便我可以构建数据集.
I have a small issue with a python program that I wrote to extract some information from a special text file. The loop (code below) needs to execute my function extract_zcoords()
over 500 files (1 file gives one list) so that I can build a dataset.
import os
def extract_zcoord(filename):
f = open(filename, 'r')
... # do something with f
### LOOP OVER DIRECTORY
location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels'
for filename in os.listdir(location):
extract_zcoord(filename)
<小时>
错误:IOException No such file or directory
是发生的那个,所以由于某种原因 python 没有访问这些文件.我检查了目录路径名(位置)和文件权限,它们是正确的(读+写).当文件确实存在且路径名正确时,为什么会报告 IOError 任何想法?
THE ERROR:
The IOException No such file or directory
is the one that occurs, so for some reason python is not accessing the files. I have checked directory pathname (location) and file permissions, and they are correct (read+write). Any ideas why an IOError would be reported when the files do exist and pathname is correct?
任何想法可能是错误的?
Any ideas what might be wrong?
推荐答案
需要将目录名和文件名连成一个完整路径:
You need to join the dirname and filename into one complete path:
location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels'
for filename in os.listdir(location):
filename = os.path.join(location, filename)
这篇关于尝试打开现有文件时出现 IOError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!