Help needed: file writing problem with subprocess

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pekka Niiranen

    #1

    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.p y" starts --

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

    then later:

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

    This fails: "myfile.txt " is not yet written into disk
    and the "myscript.p y" 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-
  • Fredrik Lundh

    #2
    Re: Help needed: file writing problem with subprocess

    Pekka Niiranen wrote:
    [color=blue]
    > 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.p y" starts --
    >
    > fh = codecs.open("my file.txt", "w", "utf8")
    > fh.write("# Comment line\n")
    > fh.writelines(" some list")
    > fh.flush()
    > fh.close()
    >
    > then later:
    >
    > mycmd = "%s -f %s" % ("myscript.p y", "myfile.txt ")
    > subprocess.call (mycmd, shell=True)
    >
    > This fails: "myfile.txt " is not yet written into disk
    > and the "myscript.p y" called thru subprocess fails.
    > Only after the main script exits will "myfile.txt " occur
    > into the directory.[/color]

    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>



    Comment

    • pekka niiranen

      #3
      Re: SOLVED: Help needed: file writing problem with subprocess

      Fredrik Lundh wrote:[color=blue]
      > Pekka Niiranen wrote:
      >
      >[color=green]
      >>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.p y" starts --
      >>
      >>fh = codecs.open("my file.txt", "w", "utf8")
      >>fh.write("# Comment line\n")
      >>fh.writelines ("some list")
      >>fh.flush()
      >>fh.close()
      >>
      >>then later:
      >>
      >>mycmd = "%s -f %s" % ("myscript.p y", "myfile.txt ")
      >>subprocess.ca ll(mycmd, shell=True)
      >>
      >>This fails: "myfile.txt " is not yet written into disk
      >>and the "myscript.p y" called thru subprocess fails.
      >>Only after the main script exits will "myfile.txt " occur
      >>into the directory.[/color]
      >
      >
      > 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>
      >
      >
      >[/color]
      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-

      Comment

      • Pekka Niiranen

        #4
        Re: SOLUTION Help needed: file writing problem with subprocess

        Fredrik Lundh wrote:[color=blue]
        > Pekka Niiranen wrote:
        >
        >[color=green]
        >>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.p y" starts --
        >>
        >>fh = codecs.open("my file.txt", "w", "utf8")
        >>fh.write("# Comment line\n")
        >>fh.writelines ("some list")
        >>fh.flush()
        >>fh.close()
        >>
        >>then later:
        >>
        >>mycmd = "%s -f %s" % ("myscript.p y", "myfile.txt ")
        >>subprocess.ca ll(mycmd, shell=True)
        >>
        >>This fails: "myfile.txt " is not yet written into disk
        >>and the "myscript.p y" called thru subprocess fails.
        >>Only after the main script exits will "myfile.txt " occur
        >>into the directory.[/color]
        >
        >
        > 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>
        >
        >
        >[/color]
        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-

        Comment

        • Pekka Niiranen

          #5
          Re: SOLUTION Help needed: file writing problem with subprocess

          Fredrik Lundh wrote:[color=blue]
          > Pekka Niiranen wrote:
          >
          >[color=green]
          >>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.p y" starts --
          >>
          >>fh = codecs.open("my file.txt", "w", "utf8")
          >>fh.write("# Comment line\n")
          >>fh.writelines ("some list")
          >>fh.flush()
          >>fh.close()
          >>
          >>then later:
          >>
          >>mycmd = "%s -f %s" % ("myscript.p y", "myfile.txt ")
          >>subprocess.ca ll(mycmd, shell=True)
          >>
          >>This fails: "myfile.txt " is not yet written into disk
          >>and the "myscript.p y" called thru subprocess fails.
          >>Only after the main script exits will "myfile.txt " occur
          >>into the directory.[/color]
          >
          >
          > 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>
          >
          >
          >[/color]
          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-

          Comment

          Working...