[Help] Closing files

Oliver Hofmann a2619725 at uni-koeln.de
Sat Aug 5 16:07:18 EDT 2000


Greetings everyone,



currently fiddling with a python script and running into some problems.
It _seems_ as if Python does not close a file properly, but I am quite sure
the problem is somewhere within my code.

Here's a snippet of the relevant code:

---8<----
text = string.join(unique, " ")

file = open("/home/setar/scripts/tmp_output", "w")
file.write(text)
file.close

time.sleep(10)

cmd = "perl /home/setar/scripts/stem.pl /home/setar/scripts/tmp_output >" +\
      "/home/setar/scripts/tmp_stemmed1"

os.popen(cmd)

time.sleep(10)
sys.exit()

---8<----

What happens is that the output of cmd (tmp_stemmed1) differs when run
in this way from the output when running the perl-script manually.

Added the sleeps to see this happening in the script - dir:

---8<----

# Before running the script, nothing in there:

setar at oho:~/scripts > l tmp*
ls: tmp*: Datei oder Verzeichnis nicht gefunden


# During the first sleep, tmp_output has been created 

setar at oho:~/scripts > l tmp*
  28 -rw-r--r--   1 setar    users       28672 Aug  5 21:16 tmp_output


# During execution of os.popen (the external perl script), runs for a few
# seconds

setar at oho:~/scripts > l tmp*
  28 -rw-r--r--   1 setar    users       28672 Aug  5 21:16 tmp_output
  20 -rw-r--r--   1 setar    users       20480 Aug  5 21:16 tmp_stemmed1
setar at oho:~/scripts > l tmp*
  28 -rw-r--r--   1 setar    users       28672 Aug  5 21:16 tmp_output
  24 -rw-r--r--   1 setar    users       24088 Aug  5 21:16 tmp_stemmed1

# Done, sleeping for the second time:

setar at oho:~/scripts > l tmp*
  28 -rw-r--r--   1 setar    users       28672 Aug  5 21:16 tmp_output
  24 -rw-r--r--   1 setar    users       24088 Aug  5 21:16 tmp_stemmed1


# Python script exits as sys.exit() and tmp_output changes

setar at oho:~/scripts > l tmp*
  32 -rw-r--r--   1 setar    users       32614 Aug  5 21:16 tmp_output
  24 -rw-r--r--   1 setar    users       24088 Aug  5 21:16 tmp_stemmed1
 
---

I've got no idea why something is written to tmp_output at this time. Of
course, now manually starting the perl script with tmp_output as an 
argument produces the correct result (which differs from tmp_stemmed1).

What I am currently doing is simple as well: Open a different file and read
from it, this seems to force a close of my actual output file.

Adding an os.popen("sync") at various times didn't help much, though;
does not seem to be related to the os.
 
All of this is on a linux system using python 1.5.2


Any help/pointer would be highly appreciated.


Thanks a lot in advance,


	Oliver






More information about the Python-list mailing list