473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help needed: file writing problem with subprocess

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-
Dec 4 '05 #1
4 1528
Pekka Niiranen wrote:
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.


this should of course work, and it sure works for me.

does this involve any unconventional file systems? (file servers etc).

how much later is later ?

have you checked what the current directory is in all three cases? (before
you create the file, before you call the script, and before you try to open the
file in the script).

</F>

Dec 5 '05 #2
Fredrik Lundh wrote:
Pekka Niiranen wrote:

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.

this should of course work, and it sure works for me.

does this involve any unconventional file systems? (file servers etc).

how much later is later ?

have you checked what the current directory is in all three cases? (before
you create the file, before you call the script, and before you try to open the
file in the script).

</F>

The problem was that full pathname to mytext.txt -file was wrong and my
handmade error message about it was misleading. Furthermore, Windows
Explorer showed created file on screen after considerable delay
(5sec) which made me think file was never created. Seems, that due to
operating system delays one cannot rely on his own eyes about
files true existence during script run.

Just one question more; how can I spawn/open another Dos -window
with subprocess()?

Thanks anyways,

-pekka-
Dec 5 '05 #3
Fredrik Lundh wrote:
Pekka Niiranen wrote:

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.

this should of course work, and it sure works for me.

does this involve any unconventional file systems? (file servers etc).

how much later is later ?

have you checked what the current directory is in all three cases? (before
you create the file, before you call the script, and before you try to open the
file in the script).

</F>

The problem was full pathname to mytext.txt -file was wrong and my
handmade error message ("expect" part) about it was misleading.
Furthermore, Windows Explorer displayed created file on screen after
considerable delay (5sec) which made me think file was never created.

Due to operating system delays one cannot rely on his own
eyes about files true existence during script's run;).

Thanks anyways,

-pekka-
Dec 5 '05 #4
Fredrik Lundh wrote:
Pekka Niiranen wrote:

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.

this should of course work, and it sure works for me.

does this involve any unconventional file systems? (file servers etc).

how much later is later ?

have you checked what the current directory is in all three cases? (before
you create the file, before you call the script, and before you try to open the
file in the script).

</F>

The problem was full pathname to mytext.txt -file was wrong and my
handmade error message ("expect" part) about it was misleading.
Furthermore, Windows Explorer displayed created file on screen after
considerable delay (5sec) which made me think file was never created.

Due to operating system delays one cannot rely on his own
eyes about files true existence during script's run;).

Thanks anyways,

-pekka-

Dec 5 '05 #5

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

Similar topics

2
by: chuck | last post by:
I need to execute a command shell process obtain the return code and capture stdout from that shell process. I've done this with 2.4 using subprocess. How do I do it with 2.3?
0
by: IamIan | last post by:
I've had to migrate back to Python 2.1 and am now trying to use os.spawnv to get around a memory leak (either in Python or ArcGIS or both) in a geoprocessing script. This script (Second Script)...
5
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend...
2
by: abcd | last post by:
I have a class which extends 'file' .... class MyFile(file): def __init__(self, fname, mode='r'): file.__init__(self, fname, mode) def write(self, str): print "writing a string"...
2
by: jool | last post by:
I have this code: cmd_output =subprocess.Popen(, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate() log.debug(cmd)) I have a logger defined, and all the...
2
by: smitty1e | last post by:
The first print statement does what you'd expect. The second print statement has rather a lot of rat in it. The goal here is to write a function that will return the man page for some command...
4
by: JD | last post by:
Hi, What I am trying to do is to run a subprocess on another machine using subprocess.Popen, this subprocess contuinue writing something into a file when it is runing. After submit this...
1
by: sven _ | last post by:
Keywords: subprocess stdout stderr unbuffered pty tty pexpect flush setvbuf I'm trying to find a solution to <URL:http://bugs.python.org/issue1241>. In short: unless specifically told not to,...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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,...

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.