473,473 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

file object: seek and close?

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
Jul 18 '05 #1
5 2549
Shu-Hsien Sheu wrote:
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
Jul 18 '05 #2
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
----------------------------------------------------------------------------------------------------------------------------------------------

Jul 18 '05 #3
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
----------------------------------------------------------------------------------------------------------------------------------------------
Jul 18 '05 #4
Shu-Hsien Sheu wrote:
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
Jul 18 '05 #5

"Peter Otten" <__*******@web.de> wrote in message
news:bk*************@news.t-online.com...
Shu-Hsien Sheu wrote:
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

Peter

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
2
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
3
by: Debbie Carter | last post by:
Can XML files be easily encrypted?
19
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also...
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
2
by: tgiles | last post by:
Hi, All! I started back programming Python again after a hiatus of several years and run into a sticky problem that I can't seem to fix, regardless of how hard I try- it it starts with tailing a...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.