file object: seek and close?

S

Shu-Hsien Sheu

Hi,

Does the seek method would close the file object after using a for loop?
My program looks like this:

f = open('somefile', 'r')
for lines in f:
some operations
f.seek(0)

for lines in f:
other operations

Python gave me an IOErros saying that the file object is already closed.
I am now opening the file again under another file object.

Am I doing something wrong or it's a bug?
Thanks!

-shuhsien
 
P

Peter Otten

Shu-Hsien Sheu said:
Does the seek method would close the file object after using a for loop?
My program looks like this:

f = open('somefile', 'r')
for lines in f:
some operations
f.seek(0)

for lines in f:
other operations

Python gave me an IOErros saying that the file object is already closed.
I am now opening the file again under another file object.

Am I doing something wrong or it's a bug?

The code you posted works here (I substituted "other operations" with "print
lines"). Please reduce your actual code to the minimum that still fails and
repost together with cut and pasted traceback.

Peter
 
S

Shu-Hsien Sheu

Hi,

I did some tests and it seems that it was the line that I defined list
as a pdbdict0 object was causing the problem.

If I remove the line:

list = pdbdict0()

there would be no IOError messages.

I tried defining list as a dictionary, and there was IOError as well.
However, if I defined list as a list, or defined any variable other than
'list' as dict, it would be just fine. Really weird.

If I remove the seek method, there would be no IOError message either.

It seems that, f.seek plus ' list = somekind of dictionary' caused the
file object to close.

Great thanks!

-shuhsien

----------------------------------------------------------------------------------------------------------------------------

class pdbdict0(dict):
def __init__(self):
super(dict, self).__init__()
self['header'] = ''
self['compnd'] = []

# read the pdb file
f = open('Z:\\3.4.21.1\\1acb.pdb', 'r')

# check if the file is an old type pdb (type = 0) or a new type pdb
(type = 1)
type = 0
for lines in f:
if lines[:6] == 'TITLE':
type = 1
if lines[:6] == 'COMPND':
break

f.seek(0)


# read in the information

if type == 0:
list = pdbdict0() # << this line seems to be causing the file
object to close
for lines in f:
lines = lines[:70]
...........................................................................

Traceback (most recent call last):
File "C:\Documents and Settings\sheu\My
Documents\precise\parspdbtest0.py", line 27, in -toplevel-
for lines in f:
ValueError: I/O operation on closed file
----------------------------------------------------------------------------------------------------------------------------------------------
 
S

Shu-Hsien Sheu

Hi,

I did some tests and it seems that it was the line that I defined list
as a pdbdict0 object was causing the problem.

If I remove the line:

list = pdbdict0()

there would be no IOError messages.

I tried defining list as a dictionary, and there was IOError as well.
However, if I defined list as a list, or defined any variable other than
'list' as dict, it would be just fine. Really weird.

If I remove the seek method, there would be no IOError message either.

It seems that, f.seek plus ' list = somekind of dictionary' caused the
file object to close.

Great thanks!

-shuhsien

----------------------------------------------------------------------------------------------------------------------------

class pdbdict0(dict):
def __init__(self):
super(dict, self).__init__()
self['header'] = ''
self['compnd'] = []

# read the pdb file
f = open('Z:\\3.4.21.1\\1acb.pdb', 'r')

# check if the file is an old type pdb (type = 0) or a new type pdb
(type = 1)
type = 0
for lines in f:
if lines[:6] == 'TITLE':
type = 1
if lines[:6] == 'COMPND':
break

f.seek(0)


# read in the information

if type == 0:
list = pdbdict0() # << this line seems to be causing the file
object to close
for lines in f:
lines = lines[:70]
...........................................................................

Traceback (most recent call last):
File "C:\Documents and Settings\sheu\My
Documents\precise\parspdbtest0.py", line 27, in -toplevel-
for lines in f:
ValueError: I/O operation on closed file
----------------------------------------------------------------------------------------------------------------------------------------------
 
P

Peter Otten

Shu-Hsien Sheu said:
Hi,

I did some tests and it seems that it was the line that I defined list
as a pdbdict0 object was causing the problem.

If I remove the line:

list = pdbdict0()

This is really strange. I still cannot reproduce the error.
class pdbdict0(dict):
def __init__(self):
super(dict, self).__init__()
self['header'] = ''
self['compnd'] = []

Wouldn't that be:

class pdbdict0(dict):
def __init__(self):
super(pdbdict0, self).__init__(self)
self['header'] = ''
self['compnd'] = []

or better:

def bdbdict0():
return {"header": "", "compnd": []}

While the pdbdict0 implementation seems broken, I have no clue what causes
the file to be closed.

Totally unrelated: Don't use list and type as variable names. They are
already used as built-in classes.

Peter
 
J

John Roth

Peter Otten said:
Shu-Hsien Sheu said:
Hi,

I did some tests and it seems that it was the line that I defined list
as a pdbdict0 object was causing the problem.

If I remove the line:

list = pdbdict0()

This is really strange. I still cannot reproduce the error.
class pdbdict0(dict):
def __init__(self):
super(dict, self).__init__()
self['header'] = ''
self['compnd'] = []

Wouldn't that be:

class pdbdict0(dict):
def __init__(self):
super(pdbdict0, self).__init__(self)
self['header'] = ''
self['compnd'] = []

or better:

def bdbdict0():
return {"header": "", "compnd": []}

While the pdbdict0 implementation seems broken, I have no clue what causes
the file to be closed.

Totally unrelated: Don't use list and type as variable names. They are
already used as built-in classes.

Actually, that's probably the problem right there. Never, ever use anything
in the built-in namespace as a variable name.

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top