Launch a shell command with in a python script, wait for the termination and return to the script

subprocess: The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. http://docs.python.org/library/subprocess.html Usage: import subprocess process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) process.wait() print process.returncode

Is it possible to run function in a subprocess without threading or writing a separate file/script.

I think you’re looking for something more like the multiprocessing module: http://docs.python.org/library/multiprocessing.html#the-process-class The subprocess module is for spawning processes and doing things with their input/output – not for running functions. Here is a multiprocessing version of your code: from multiprocessing import Process, Queue # must be a global function def my_function(q, x): q.put(x + 100) … Read more

Python popen command. Wait until the command is finished

Depending on how you want to work your script you have two options. If you want the commands to block and not do anything while it is executing, you can just use subprocess.call. #start and block until done subprocess.call([data[“om_points”], “>”, diz[‘d’]+”/points.xml”]) If you want to do things while it is executing or feed things into … Read more

Why does Popen.communicate() return b’hi\n’ instead of ‘hi’?

The b indicates that what you have is bytes, which is a binary sequence of bytes rather than a string of Unicode characters. Subprocesses output bytes, not characters, so that’s what communicate() is returning. The bytes type is not directly print()able, so you’re being shown the repr of the bytes you have. If you know … Read more

What’s the difference between subprocess Popen and call (how can I use them)?

There are two ways to do the redirect. Both apply to either subprocess.Popen or subprocess.call. Set the keyword argument shell = True or executable = /path/to/the/shell and specify the command just as you have it there. Since you’re just redirecting the output to a file, set the keyword argument stdout = an_open_writeable_file_object where the object … Read more

How can I specify working directory for popen

subprocess.Popen takes a cwd argument to set the Current Working Directory; you’ll also want to escape your backslashes (‘d:\\test\\local’), or use r’d:\test\local’ so that the backslashes aren’t interpreted as escape sequences by Python. The way you have it written, the \t part will be translated to a tab. So, your new line should look like: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)