How to alternate around directories using subprocess - python

I want to change the current directory using subprocess.
For example:
import os, sys, subprocess
os.environ['a'] = '/home'
os.environ['b'] = '/'
subprocess.call('cd $a', shell=True)
subprocess.call('ls', shell=True)
subprocess.call('cd $b', shell=True)
subprocess.call('ls', shell=True)
I think that this should work like a command line unix
$ export a='/home'
$ export b='/'
$ cd $a
$ ls
$ cd $b
$ ls
But it doesn't happen..
How must I do to change the current dir?
Thanks.

To change the directory just use os.chdir() instead.
You can also execute commands in specific directoeies by running subprocess.Popen(...) - it has an optional parameter cwd=None. Just use it to specify the working directory.
Also, you could take a look at a small module I wrote that completes some missing functionality from Python standard library. Probably this module especially https://github.com/ssbarnea/tendo/blob/master/tendo/tee.py

Related

How to use variables in subprocess executing multiple commands in single session using python

I want to run the multiple shell commands using subprocess in python. The commands contains variables as well.
Below is the code that I have tried, But it is not working.
import subprocess
fdr = "build"
dir = "temp"
subprocess.call("cd "+fdr+";mkdir "+dir+";cd "+dir+";pwd", shell=True)
fdr, dir are the variables.
how can I do this?
There is a little problem with &&. Try this:
import subprocess
fdr = "build"
dir = "temp"
subprocess.call("cd "+fdr+" && mkdir "+dir+" && cd "+dir+" && pwd", shell=True)
The && in bash calls other command only if the first was successful. It works on my machine (unfortunately I have only Windows cmd right now so I've made some changes for my version but It should works the same).

Use the path as an argument in shell file from python

I want to call from python a shell script which contain the running of another python function. I would like to use for that subprocess method. My code so far look like:
arguments = ["./my_shell.sh", path]
ret_val = subprocess.Popen(arguments, stdout=subprocess.PIPE)
while the script is the following:
#!/bin/sh
cd ...
python -c "from file import method;
method()"
How can I give in the directory (to cd) of the path that I pass as an argument in the shell file?
You can access your arguments as $1, $2, etc. So your cd command would simply be cd $1.

Sending CMD command prompts using Subprocess (Python)

This is a beginner level question for anyone pro in subprocess.
In Windows, is it possible for me to send the following CMD commands using subprocesssuch that they are executed one after another in a single shell:
cd C:\Users\User\myvirtualenvs\project1
Scripts\activate.bat
Hello.py
Effectively, I am trying to load the Virtualenv without having to manually myself touch CMD prompt.
Thanks in advance :)
Just like mentioned in the Comment with &&:
from subprocess import call
call(r'cd C:\ && echo 123 && dir', shell=True)
Please notice the shell=True argument.
Edit due to comment:
Shell=True is an security issue, if you're passing raw input values to the call. See this example from the docs:
from subprocess import call
filename = input("What file would you like to display?\n")
>>> What file would you like to display?
>>> non_existent; rm -rf / #
call("cat " + filename, shell=True) # Uh-oh. This will end badly...
In initially thought you want to make a small script for personal purposes. If you want to give this code away, think about packaging your code via distutils or setuptools.

Calling a subprocess in python with environmental variables

I am trying to write a python script to automatically scan a section of plex using the Plex Media Scanner. To do so, I must run the scanner as the user running plex (in this case it is 'plex') as well as provide it with the environment variable 'LD_LIBRARY_PATH'. I've tried using both subprocess.call and subprocess.Popen with no difference. In either case, I am not getting any output.
Here is the code I am using:
#!/usr/bin/python
import os
import subprocess
import shlex
env = os.environ.copy()
env['LD_LIBRARY_PATH'] = '/usr/lib/plexmediaserver'
s = "/bin/su - plex -c '/usr/lib/plexmediaserver/Plex\ Media\ Scanner -s -c 2'"
task = shlex.split(s)
exitCode = subprocess.call(task, env=env, shell=True)
Now I already have a working version that does what I want it to do but I had to resort to using a wrapper bash script to do so. You can see the code below:
#!/bin/sh
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver
/usr/lib/plexmediaserver/Plex\ Media\ Scanner $#
And the relevant line of the script which calls it:
exitCode = subprocess.call("/bin/su - plex -c '/var/lib/deluge/delugeScripts/pms.sh -s -c 2'", shell=True)
Thanks for your help.
As jordanm noted in his comment:
the - in su makes it a login shell which re-initializes the environment.

How to make a call to an executable from Python script?

I need to execute this script from my Python script.
Is it possible? The script generate some outputs with some files being written. How do I access these files? I have tried with subprocess call function but without success.
fx#fx-ubuntu:~/Documents/projects/foo$ bin/bar -c somefile.xml -d text.txt -r aString -f anotherString >output
The application "bar" also references to some libraries, it also create the file "bar.xml" besides the output. How do I get access to these files? Just by using open()?
Thank you,
Edit:
The error from Python runtime is only this line.
$ python foo.py
bin/bar: bin/bar: cannot execute binary file
For executing the external program, do this:
import subprocess
args = ("bin/bar", "-c", "somefile.xml", "-d", "text.txt", "-r", "aString", "-f", "anotherString")
#Or just:
#args = "bin/bar -c somefile.xml -d text.txt -r aString -f anotherString".split()
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print output
And yes, assuming your bin/bar program wrote some other assorted files to disk, you can open them as normal with open("path/to/output/file.txt"). Note that you don't need to rely on a subshell to redirect the output to a file on disk named "output" if you don't want to. I'm showing here how to directly read the output into your python program without going to disk in between.
The simplest way is:
import os
cmd = 'bin/bar --option --otheroption'
os.system(cmd) # returns the exit status
You access the files in the usual way, by using open().
If you need to do more complicated subprocess management then the subprocess module is the way to go.
For executing a unix executable file. I did the following in my Mac OSX and it worked for me:
import os
cmd = './darknet classifier predict data/baby.jpg'
so = os.popen(cmd).read()
print so
Here print so outputs the result.

Categories

Resources