在每次遇到错误时运行下面的代码:
"OSError: Errno 9坏文件描述符“
在f.flush()和f.close()行。注释掉f.flush()和f.close()行似乎修复了这个错误,但我仍然不明白为什么会出现这个问题。
import numpy as np
#import math
import os
import matplotlib.pyplot as plt
import networkx as nex
def creator(self): #Creating an Adjaceny Matrix From Given CSV
print("AdjacenyList\%s" %(self.string))
if(os.path.isdir("AdjacenyList\%s" %(self.string))==False):
os.mkdir("AdjacenyList\%s" %(self.string))
os.chdir("AdjacenyList\%s" %(self.string))
f=open("%s_AdjList.txt" %(self.string), 'w')
for i in range(0, len(self.foodweb)):
m=0
f.write("%d" %(i))
for j in self.foodweb[i,:]:
if j>0:
f.write(" %d" %(m))
m+=1
f.write("\n")
f.flush()
f.close()另外,当我以'rb‘模式打开另一个文件(在同一个脚本中)并在读取它之后刷新和关闭它时,没有问题。任何帮助都将不胜感激。
def plotter(self):
print(self.foodweb.shape)
g=open("%s_AdjList.txt" %(self.string),'rb')
self.DirGraph=nex.read_adjlist(g, create_using=nex.DiGraph)
nex.draw_networkx(self.DirGraph)
plt.axis('off')
plt.show()
plt.close()
g.flush()
g.close()发布于 2019-05-20 10:16:32
试着打开完整的路径
"AdjacenyList\%s\s_AdjList.txt" %(self.string)你错过了\试试f = open("\%s_AdjList.txt" % (self.string), 'w')
请将"AdjacenyList\%s" %(self.string)和打开的目录"\%s_AdjList.txt" % (self.string)提取为变量并打印它们。并确认它们是一样的?(不包括文件名)?
https://stackoverflow.com/questions/56218731
复制相似问题