Help needed: file writing problem with subprocess

Pekka Niiranen pekka.niiranen at wlanmail.com
Sun Dec 4 17:47:08 EST 2005


Hi,

I am running Python script in W2K or in WinXP.
The script is started from DOS prompt and writes text file to disk
with codecs.open() in UTF8.

The problem is: When script writes the file and tries to read it
with by calling itself thru subprocess() the created files are
NOT accessible because they have not been written into disk yet.
fh.flush() and os.fsync() do not work.

-- extract from "myscript.py" starts --

fh = codecs.open("myfile.txt", "w", "utf8")
fh.write("# Comment line\n")
fh.writelines("some list")
fh.flush()
fh.close()

then later:

mycmd = "%s -f %s" % ("myscript.py", "myfile.txt")
subprocess.call(mycmd, shell=True)

This fails: "myfile.txt" is not yet written into disk
and the "myscript.py" called thru subprocess fails.
Only after the main script exits will "myfile.txt" occur
into the directory.

Previously I had Perl script calling shell script
thru "system" -command. My plan is to rewrite everything with Python.

-pekka-



More information about the Python-list mailing list