problem with multiple read/write

6 views
Skip to first unread message

rudnik

unread,
Aug 26, 2011, 11:28:58 AM8/26/11
to shedskin-discuss
writes.py:

linia=[]

def zapisz():
plik=file(nazwa,"w")
for line in linia:
plik.write(line)
del plik

def czytaj():
del linia[:]
plik=file(nazwa)
for line in plik.readlines():
if line!="\n" and line!="\r" and line!="\r\n":
linia.append(line)
del plik

nazwa="plik.txt"
czytaj()
linia+=["l1","l2","l3"]
zapisz()
czytaj()
linia+=["asdf"]
zapisz()



plik.txt:
text1

If I use CPython, I get in plik.txt:
tekst1
l1l2l3asdf

If I use Shedskin version of my program, I get in plik.txt:
tekst1
l1l2l3

Mark Dufour

unread,
Aug 26, 2011, 1:48:45 PM8/26/11
to shedskin...@googlegroups.com

If I use CPython, I get in plik.txt:
tekst1
l1l2l3asdf

If I use Shedskin version of my program, I get in plik.txt:
tekst1
l1l2l3

thanks for reporting. please do use the issue tracker in the future.. :-)

the problem is that you are relying on garbage collection/reference counting here to close the file.. which can be seen as something implementation dependent, as well as undesirable according to the second line of 'import this'.. :-)

so the workaround is to explicitly close the file using 'plik.close()'.

thanks again,
mark.
--
http://www.youtube.com/watch?v=E6LsfnBmdnk

Mark Dufour

unread,
Aug 28, 2011, 2:17:10 PM8/28/11
to shedskin...@googlegroups.com
def zapisz():
   plik=file(nazwa,"w")
   for line in linia:
      plik.write(line)
   del plik


this last line should now cause a warning. thanks for triggering! :)

mark.
--
http://www.youtube.com/watch?v=E6LsfnBmdnk

Reply all
Reply to author
Forward
0 new messages