1

i got this list

commands = ['cd var','cd www','cd html','sudo rm -r folder']

I'm trying to execute one by one all the elements inside as a bash script, with no success. Do i need a for loop here?

how to achieve that?, thanks all!!!!

6
  • Possible duplicate of running multiple bash commands with subprocess Commented Nov 15, 2018 at 0:46
  • 1
    You could condense the cd commands into one command, then use Bash's && (and) operator: os.system("cd var/www/html/ && sudo rm -r folder") Commented Nov 15, 2018 at 1:04
  • What does Python have to do with this? You said "execute as a bash script". Commented Nov 15, 2018 at 1:34
  • 1
    Possible duplicate of Delete a file or folder Commented Nov 15, 2018 at 5:57
  • 1
    Maybe see also stackoverflow.com/a/51950538/874188 for a number of things to look out for with subprocesses. Commented Nov 15, 2018 at 6:23

3 Answers 3

5
for command in commands:
    os.system(command)

is one way you could do it ... although just cd'ing into a bunch of directories isnt going to have much impact

NOTE this will run each command in its own subshell ... so they would not remember their state (ie any directory changes or environmental variables)

if you need to run them all in one subshell than you need to chain them together with "&&"

os.system(" && ".join(commands)) # would run all of the commands in a single subshell

as noted in the comments, in general it is preferred to use subprocess module with check_call or one of the other variants. however in this specific instance i personally think that you are in a 6 to 1 half a dozen to the other, and os.system was less typing (and its gonna exist whether you are using python3.7 or python2.5 ... but in general use subprocess exactly which call probably depends on the version of python you are using ... there is a great description in the post linked in the comments by @triplee why you should use subprocess instead)

really you should reformat your commands to simply

commands = ["sudo rm -rf var/www/html/folder"] note that you will probably need to add your python file to your sudoers file

also Im not sure exactly what you are trying to accomplish here ... but i suspect this might not be the ideal way to go about it (although it should work...)

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

5 Comments

This won't work because cd var will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process.
the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour
No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses.
Alao you should prefer subprocess.run() or subprocess.check_call() over os.system(); see the post I linked from another comment above.
none of the compelling reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require shell=True so its pretty extra moot ... that said in general yes... this was just less typing
3

This is just a suggestion, but if your just wanting to change directories and delete folders, you could use os.chdir() and shutil.rmtree():

from os import chdir
from os import getcwd
from shutil import rmtree

directories = ['var','www','html','folder']

print(getcwd())
# current working directory: $PWD

for directory in directories[:-1]:
    chdir(directory)

print(getcwd())
# current working directory: $PWD/var/www/html

rmtree(directories[-1])

Which will cd three directories deep into html, and delelte folder. The current working directory changes when you call chdir(), as seen when you call os.getcwd().

Comments

2
declare -a command=("cd var","cd www","cd html","sudo rm -r folder")

## now loop through the above array
for i in "${command[@]}"
do
echo "$i"
# or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also

2 Comments

this is awesome :) I think they were asking how to do it in python but this is great! thanks!
A python list can be very easily imported in to bash stackoverflow.com/questions/26162394/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.