Python - ValueError: I/O operation on closed file.

 
Vista:

ValueError: I/O operation on closed file.

Publicado por David (2 intervenciones) el 29/05/2014 20:35:02
Buenas, tengo el siguiente codigo que es parte de un compilador

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Componente:
	diccionario = []
	def leer(self, nombreArchivo):
		archi=open(nombreArchivo,'r')
		linea = archi.readline()
		indice = 0
		while linea!="":
			self.diccionario.insert(indice,linea.rstrip('\n'))
			linea=archi.readline()
			archi.close()
	def escribir(self, nombreArchivo):
		f = open(nombreArchivo,'w')
		for variable in prueba.diccionario:
			f.write(variable+"/n")
			f.close
	def configurar(self):
		print ("Esta funcion no tiene su funcionalidad")
	def procesar(self):
		print ("Esta funcion no tiene su funcionalidad")
class AnLexico(Componente):
	validos = []
	def configurar(self,nombreArchivo):
		archi=open(nombreArchivo,'r')
		linea = archi.readline()
		self.validos.insert(0,' ')
		indice = 1
		while linea!="":
			self.validos.insert(indice,linea.rstrip('\n'))
			linea=archi.readline()
		archi.close()
 
	def procesar(self):
		for token in prueba.diccionario:
			bandera = False;
			for tokenvalido in prueba.validos:
				if token == tokenvalido:
					bandera = True
					break
			if bandera == False:
				return False
		return True
 
 
prueba = AnLexico()
prueba.leer("EjemploMorse.txt")
prueba.configurar("ConfigurarLexico.txt")
if prueba.procesar():
	prueba.escribir("salida.txt")
else:
	print ("No se pudo escribir el archivo")

Pero a la hora de ejecutarlo me marca el siguiente error

Traceback (most recent call last):
File "compilador.py", line 45, in <modul
prueba.leer("EjemploMorse.txt")
File "compilador.py", line 9, in leer
linea=archi.readline()
ValueError: I/O operation on closed file.

Y no encuentro porqué. Gracias por la ayuda
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 2.239
Plata
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

ValueError: I/O operation on closed file.

Publicado por xve (1646 intervenciones) el 29/05/2014 22:24:28
Hola David, creo que tu problema, es porque llegas al final del archivo, y sigues intentado leer una linea mas que no existe.

Antes del error, te ha capturado el contenido del archivo?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar