1

I want to read the content of the file which was written to the file by different function

from subprocess import *
import os
def compile():
    f=open("reddy.txt","w+")
    p=Popen("gcc -c rahul.c ",stdout=f,shell=True,stderr=STDOUT) #i have even tried with with open but it is not working,It is working with r+ but it is appending to file.
    f.close()   

def run():
    p1=Popen("gcc -o r.exe rahul.c",stdout=PIPE,shell=True,stderr=PIPE)
    p2=Popen("r.exe",stdout=PIPE,shell=True,stderr=PIPE)
    print(p2.stdout.read())
    p2.kill()

compile()
f1=open("reddy.txt","w+")    
first_char=f1.readline() #unable to read here ….!!!!!!
print(first_char)

  #run()

first_char must have first line of file reddy.txt but it is showing null

  • if you want read and write to a file then use `r+` not `w+` – Albin Paul Apr 05 '19 at 05:36
  • I will write to file using compile function i.e "w+" than I am trying to read using "r+" but even then that is not possible – Rahul Lenkala Apr 05 '19 at 05:42
  • Please tell us what error you get. – 9769953 Apr 05 '19 at 06:15
  • my compile fnction will compile Rahul .c using gcc and write error in reddy.txt file than after I must read the file and print the content of the file but I am unable to read the file once my compile function is done with writing – Rahul Lenkala Apr 05 '19 at 06:17
  • That's a description of your program; that is not an error message, traceback or anything. "unable to read" is too unclear, you need to be more specific. – 9769953 Apr 05 '19 at 06:47

2 Answers2

1

You are assuming that Popen finishes the process, but it doesn't; Popen will merely start a process - and unless the compilation is extremely fast, it's quite likely that reddy.txt will be empty when you try to read it.

With Python 3.5+ you want subprocess.run().

# Don't import *
from subprocess import run as s_run, PIPE, STDOUT
# Remove unused import
#import os

def compile():
    # Use a context manager
    with open("reddy.txt", "w+") as f:
        # For style points, avoid shell=True
        s_run(["gcc", "-c", "rahul.c "], stdout=f, stderr=STDOUT,
            # Check that the compilation actually succeeds
            check=True)

def run():
    compile()  # use the function we just defined instead of repeating youself
    p2 = s_run(["r.exe"], stdout=PIPE, stderr=PIPE,
        # Check that the process succeeds
        check = True,
        # Decode output from bytes() to str()
        universal_newlines=True)
    print(p2.stdout)

compile()
# Open file for reading, not writing!
with open("reddy.txt", "r") as f1:
    first_char = f1.readline()
print(first_char)

(I adapted the run() function along the same lines, though it's not being used in any of the code you posted.)

first_char is misleadingly named; readline() will read an entire line. If you want just the first byte, try

first_char = f1.read(1)

If you need to be compatible with older Python versions, try check_output or check_call instead of run. If you are on 3.7+ you can use text=True instead of the older and slightly misleadingly named universal_newlines=True.

For more details about the changes I made, maybe see also this.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

If you have a look at the documentation on open you can see that when you use w to open a file, it will first truncate that files contents. Meaning there will be no output as you describe.

Since you only want to read the file you should use r in the open statement:

f1 = open("reddy.txt", "r")
cullzie
  • 2,705
  • 2
  • 16
  • 21
  • It's not clear in the question where data is added to this file but if you manually check `reddy.txt` after a compile is there any data added? – cullzie Apr 05 '19 at 05:44
  • I would use the `with` keyword and call wait on Popen. Otherwise your file may be closed before the process is finished: `with open("reddy.txt", "w+") as f: Popen("gcc -c rahul.c ",stdout=f,shell=True,stderr=STDOUT).wait()` – cullzie Apr 05 '19 at 05:53
  • I have even tried using with but even then I am unable to read it – Rahul Lenkala Apr 05 '19 at 05:57
  • main intension of this program is to compile and run c program using python script – Rahul Lenkala Apr 05 '19 at 05:57
  • What do you mean you are unable to read it after using `with`? Perhaps you can update your question with the other ways you have tried to help steer us towards a useful answer. – cullzie Apr 05 '19 at 06:00
  • with open("reddy.txt", "w+") as f: Popen("gcc -c rahul.c ",stdout=f,shell=True,stderr=STDOUT).wait() even after using this syntax I am unable to read from the file – Rahul Lenkala Apr 05 '19 at 06:02
  • Have you tried deleting the txt file and re-running? – cullzie Apr 05 '19 at 06:19