0

This is baffling me.

I have a python script that does some work on a Windows platform to generate an XML file, download some images and then call an external console application to generate a video using the xml and images the python script has generated.

The application I call with pOPen is supposed to return a status i.e. [success] or [invalid] or [fail] dependant on how it interprets the data I pass it.

If I use my generation script to generate the information and then call the console application separately in another script it works fine and I get success and a video is generated.

Successful code (please ignore the test prints!):

print ("running console app...")

cmd = '"C:/Program Files/PropertyVideos/propertyvideos.console.exe" -xml data/feed2820712.xml -mpeg -q normal'
print (cmd)
p = subprocess.Popen(cmd , stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = p.communicate()[0]
print ("\n----\n[" + output + "]\n----\n")

if output == "[success]":
    print "\nHURRAHHHHH!!!!!!!"

print ("finished...")

But if I include the same code at the end of the script that generates the info to feed the console application then it runs for about 2 seconds and output = []

Same code, just ran at the end of a different script...

EDIT: Thanks to Dave, Dgrant and ThomasK it seems to be that the generate script is not closing the file as redirecting strerr to stdout shows:

Unhandled Exception: System.IO.IOException: The process cannot access the file '
C:\videos\data\feed2820712.xml' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

However I AM closing the file: Extract from the generation script:

    xmlfileObj.write('</FeedSettings>\n') # write the last line
    xmlfileObj.close

    # sleep to allow files to close
    time.sleep(10)

    # NOW GENERATE THE VIDEO
    print ("initialising video...")

    cmd = '"C:/Program Files/PropertyVideos/propertyvideos.console.exe" -xml data/feed2820712.xml -mpeg -q normal'
    print (cmd)
    p = subprocess.Popen(cmd , stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = p.communicate()[0]
    print ("\n----\n[" + output + "]\n----\n")

    if output == "[success]":
        print "\nHURRAHHHHH!!!!!!!"

    print ("finished...")   

Any help would be appreciated.

14
  • 3
    I think you either need shell=True, or to break your command up into a list of arguments. Commented Nov 22, 2011 at 21:14
  • 1
    This is unrelated, but you could do pass in stderr=subprocess.stdout to redirect it to stdout, because as it is now, you are dropping stderr. Commented Nov 22, 2011 at 21:24
  • 1
    Please show the code at the end of the generation script that successfully calls the "running console app" script. It sounds like you're closing the XML file before calling the second script, but not before reaching the included code at the end of the single script. Commented Nov 22, 2011 at 21:24
  • 2
    Is the "separate script" in the same folder? You are using relative paths here..that could be your issue. I always use absolute paths. You can change paths into absolute paths by using os.path.abspath(pathname) and another thing that is convenient to do is get the path where your script resides (different than the working folder, which is where you started the script). SCRIPT_ROOT=os.path.split(os.path.abspath(file))[0] Commented Nov 22, 2011 at 21:26
  • 1
    @dan360: It would be subprocess.STDOUT. Commented Nov 22, 2011 at 21:42

1 Answer 1

4

You're not closing the file. Your code says:

xmlfileObj.close

when it should be

xmlfileObj.close()

Edit: Just to clarify - the code xmlfileObj.close is a valid python expression which returns a reference to the built in close method of a file (or file like) object. Since it is a valid python expression it is perfectly legal code, but it does not have any side effects. Specifically, it does not have the effect of actually calling the close() method. You need to include the open and close brackets to do that.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh for the love of <insert deity here>. You stare at a piece of code for hours and just can't see something so simple! - Many thanks.
I once saw a massive codebase with fp.flush followed by fp.close littered throughout the codebase. Huge facepalm, but it turned out it never really mattered, it was mostly writing out logs that would be closed when the script exited (ie. they were never read by the script)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.