os.system vs subprocess in python on linux

2024/10/2 8:31:36

I have two python scripts. The first script calls a table of second scripts in which I need to execute a third party python script. It looks something like this:

# the call from the first script. 
cmd = "qsub -sync y -b -cwd -V -q long  -t 1-10 -tc 5 -N 'script_two' ./script2.py"script2thread = pexpect.spawn(cmd)# end of script 1 

So here i am sending 10 jobs out to the queue. In script 2 I have a case statement based on the task_id. In each one I make a similar call to the third party script using different parameters.

...
elif(task_id == 4)
subprocess.call(./script3)# or os.system(./script3 , shell=True)

This is where my question lies. Is there a difference/benefit to using one or the other? I know that on windows using one over the other makes a big difference because of support issues but I am on linux and have no intention of running this on windows. Sometimes I get very weird results from using the subprocess, it cannot find other things on the network that it can when the third script is run independently one at a time.

Answer

You should use subprocess. Not that it makes any difference, it's just a newer module intended to replace os.system (have a look at this section for a drop-in replacement). It also has more features in case you need them one day.

In short: there is no reason to use os.system (except for compatibility with older versions of Python).

https://en.xdnf.cn/q/70869.html

Related Q&A

How can I call a python script from a python script

I have a python script b.py which prints out time ever 5 sec.while (1):print "Start : %s" % time.ctime()time.sleep( 5 )print "End : %s" % time.ctime()time.sleep( 5 )And in my a.py, …

Read EXE, MSI, and ZIP file metadata in Python in Linux

I am writing a Python script to index a large set of Windows installers into a DB. I would like top know how to read the metadata information (Company, Product Name, Version, etc) from EXE, MSI and ZIP…

IllegalArgumentException thrown when count and collect function in spark

I tried to load a small dataset on local Spark when this exception is thrown when I used count() in PySpark (take() seems working). I tried to search about this issue but got no luck in figuring out wh…

Check if string does not contain strings from the list

I have the following code: mystring = ["reddit", "google"] mylist = ["a", "b", "c", "d"] for mystr in mystring:if any(x not in mystr for x in…

How do I conditionally include a file in a Sphinx toctree? [duplicate]

This question already has answers here:Conditional toctree in Sphinx(4 answers)Closed 8 years ago.I would like to include one of my files in my Sphinx TOC only when a certain tag is set, however the ob…

Use BeautifulSoup to extract sibling nodes between two nodes

Ive got a document like this:<p class="top">I dont want this</p><p>I want this</p> <table><!-- ... --> </table><img ... /><p> and all tha…

Put HTML into ValidationError in Django

I want to put an anchor tag into this ValidationError:Customer.objects.get(email=value)if self.register:# this address is already registeredraise forms.ValidationError(_(An account already exists for t…

python os.listdir doesnt show all files

In my windows7 64bit system, there is a file named msconfig.exe in folder c:/windows/system32. Yes, it must exists.But when i use os.listdir to search the folder c:/windows/system32, I didnt get the fi…

how to save modified ELF by pyelftools

Recently Ive been interested in ELF File Structure. Searching on web, I found an awesome script named pyelftools. But in fact I didnt know the way to save the modified ELF; ELFFile class doesnt have an…

Access train and evaluation error in xgboost

I started using python xgboost backage. Is there a way to get training and validation errors at each training epoch? I cant find one in the documentation Have trained a simple model and got output:[09…