Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7628657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:38:01+00:00 2026-05-31T05:38:01+00:00

I have a python script that runs a subprocess to get some data and

  • 0

I have a python script that runs a subprocess to get some data and then process it. What I’m trying to achieve is have the data written to a file, and then use the data from the file to do the processing (the reason is that the subprocess is slow, but can change based on the date, time, and parameters I use, and I need to run the script frequently)

I’ve tried various methods, including opening the file as w+ and trying to seek to the beginning after the write is done, but nothing seems to work – the file is written, but when I try to read back from it (using file.readline()) i get EOF back.

This is what I’m essentially trying to accomplish:

      myFile = open(fileName, "w")
      p = subprocess.Popen(args, stdout=myFile)
      myFile.flush()    # force the file to disk
      os.fsync(myFile)  # ..
      myFile.close()

      myFile = open(fileName, "r")
      while myFile.readline():
        pass # do stuff
      myFile.close()

But even though the file is correctly written (after the script runs, i can see the contents of the file), readline never returns a valid line. Like I said I also tried using the same file object, and doing seek(0) on it, to no luck. This only worked when opening the file as r+, which fails when the file doesn’t already exist.

Any help would be appreciated. Also if there’s a cleaner way to do this, i’m open to it 🙂

PS: I realize I can Popen and stdout to a pipe, read from the pipe and then write line by line the data to the file as I do that, but I’m trying to separate the creation of the data file from the reading.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:38:02+00:00Added an answer on May 31, 2026 at 5:38 am

    @James Aylett pointed me to the right path, it appears that my problem was that subprocess.Popen wasn’t finished running when I call .flush().

    The solution, is to call p.wait() right after the subprocess.Popen call, to allow for the underlying command to finish. After doing that, .flush does the right thing (since all the data is there), and I can proceed to read from the file.

    So the above code becomes:

      myFile = open(fileName, "w")
      p = subprocess.Popen(args, stdout=myFile)
    
      p.wait()          # <-- Missing line
    
      myFile.flush()    # force the file to disk
      os.fsync(myFile)  # ..
      myFile.close()
    
      myFile = open(fileName, "r")
      while myFile.readline():
        pass # do stuff
      myFile.close()
    

    And then it all works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that connects to an external host, fetches some data,
I have an installation script written in Python (in Linux) that runs as root
I have a python script that runs continuously. It outputs 2 lines of info
I have Jenkins running a python script that makes some SVN calls, my problem
I have a Python program that runs a series of experiments, with no data
So I have a python script that runs a loop in which it calls
I have a python script that runs on windows and uses win32 extensions and
I have a simple python script that just runs an infinite while loop and
I have a simple Python script that runs in a console windows. How can
I have a Python script that needs to process a large number of files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.