Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 🐍 Python: write to text file

    Dear All,

    below is a Stata-Python interface question.

    I need to write to a file opened in Stata from a Python procedure (executed from within Stata). My output is interleaved between various Stata and Python procedures.
    I understand that I can close the file after each write, and then open it for appending, but I would like to avoid the associated performance penalty, and more importantly, leaving a half-produced file in a closed state. So I would like to retain the handler open in Stata and just write to the file from Python reusing the same handler until the program succeeds.

    I couldn't locate the necessary output procedure in the Stata’s Python API documentation.

    My preference would be of course to have the file handler accessible from Python for read/write commands as outlined here:
    https://www.geeksforgeeks.org/readin...-files-python/

    But if there are mirroring commands available through some utility class, a la
    Code:
    StataFile.Files(handle).Write("")
    that should also be compatible.

    Is there any possibility to do it?

    Alternatively, and perhaps easier to establish, is it safe (in a sense of multithreaded access, buffering, OS permissions, etc) to re-open for writing in python the same file that has been already open for writing in the same Stata session?

    Thank you, Sergiy Radyakin


  • #2
    Solved this by forming a full Stata command in Python and executing it through the general executor.

    Comment


    • #3
      Can you share the code here
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        Originally posted by Sergiy Radyakin View Post
        Alternatively, and perhaps easier to establish, is it safe (in a sense of multithreaded access, buffering, OS permissions, etc) to re-open for writing in python the same file that has been already open for writing in the same Stata session?
        Maybe, but I can't imagine you are going to get any significant speedup in return for the increased risk. And even if it works, I wouldn't necessarily expect it to keep working the next time you update python, stata, or your OS. Also unlikely to be very portable across OSs. I would always close a file from Stata before reading in python and vice versa. Anything else is just rolling the dice IMO.

        Also, it's generally recommended nowadays in python to use the "with" context manager to automatically handle opening and closing of files when reading and writing. That may not apply if you really want to handle it yourself, as you suggest here, but again, that seems like a pretty risky plan.

        Comment


        • #5
          Attaullah Shah , as I wrote, I didn't find a way to exchange the file handle between Stata's ado-language and the python code, but since the file is first opened by Stata, it remains opened throughout the python execution, so text can be appended to it at any time. submitting the file write commands from python to stata with a proper stata handle worked out fine.

          John Eiler that was exactly the nature of my question, is it safe. The answers clearly fall into 3 groups: "no, will not work like that", "yes, will work like that", "may work for some setups/conditions and not work otherwise". You also wrote:
          I would always close a file from Stata before reading in python and vice versa.
          In my case Stata and python have to work simultaneously, since they are interleaved.

          Comment

          Working...
          X