0

I am trying to run a bash command from a python script after looking at these videos (1, 2). This is my first time trying this.

My script:

import os
import subprocess

os.system("cd Downloads/smartgit")
#  os.system("cd Downloads/smartgit/bin")
#  os.system('sudo "bin/smartgit.sh"')
#  os.system("sudo bin/smartgit.sh")
#  os.system("sudo ./smartgit.sh")
#  subprocess.call("./smargit.sh", shell=True)
#  subprocess.call("sudo ./smargit.sh", shell=True)
#  subprocess.call("sudo smargit.sh", shell=True)
subprocess.call("bin/smargit.sh", shell=True)

You can see my earlier incarnations commented out. I chmod'd the file, but neither this:

malikarumi@Tetuoan2:~/Downloads/smartgit/bin$ cd ~
malikarumi@Tetuoan2:~$ python smartgit.py
sh: 1: bin/smartgit.sh: not found

Nor this:

malikarumi@Tetuoan2:~$ python smartgit.py
sudo: bin/smartgit.sh: command not found

Worked, and I don't get why, because this:

malikarumi@Tetuoan2:~$ cd Downloads/smartgit
malikarumi@Tetuoan2:~/Downloads/smartgit$ bin/smartgit.sh

does!

Thanks for helping me understand and fix this script.

  • Why are you using Python if all you do with it is to invoke shell commands? A simple shell script would do that far easier. What exactly are you trying to achieve here? – David Foerster Sep 11 '17 at 11:28
  • Like I said, I was trying out something I saw in these videos. It is just as simple as that. I don't know anything about shell scripting. I do know a thing or two about Python, so I thought I'd try it. That's not so surprising or unusual, is it, really? It is a scripting language, and from what I can tell, people use it to glue together all kinds of other apps and systems. – Malik A. Rumi Sep 12 '17 at 19:29
  • Fair enough. If you prefer Python you may like the GitPython module (packages python-git and python3-git respectively). As long most of your Python commands make use of os.system or the subprocess module you're basically wrapping a shell script inside a Python program (with additional pitfalls) which would require you to learn some shell scripting anyway. – David Foerster Sep 12 '17 at 21:05
  • @DavidFoerster: You were right. When I took a little time to learn basic bash scripting, it worked as it is supposed to. If you want to make your comment into an answer, I will accept it. – Malik A. Rumi Sep 18 '17 at 18:53
1

Why are you using Python if all you do with it is to invoke shell commands? A simple shell script would do that far easier and as long most of your Python commands make use of os.system or the subprocess module you're basically wrapping a shell script inside a Python program (with additional pitfalls) which would require you to learn some shell scripting anyway.

Here's how you can achieve the same (as far as I understand it) with a shell script:

#!/bin/sh
cd Downloads/smartgit
bin/smargit.sh

Or more simply:

Downloads/smartgit/bin/smargit.sh
3

It makes no sense to run cd commands in Python's os.system(...) function, as each of those calls spawns its own, separate shell inside which the command runs, which do not affect the main process or the shells of other function calls. Therefore the cd of one call does not affect the working directory of other calls.

You can use os.chdir(...) instead to change the working directory of your whole Python process.

However, you should not rely on implicit relative paths like this in your application, this will break if you run the script from any other location than the home directory. Maybe you want to prefix the path with ~/ to be relative to your home directory.

3

os.system() starts a shell, executes the command, and closes that shell. Your cd's effect is lost. Change directory with Python itself:

os.chdir("Downloads/smartgit")
subprocess.call(["bin/smargit.sh"])

Better yet, which chdir at all? Why not directly call the script:

smartgit_path = os.path.expanduser("~/Download/smartgit/bin/smartgit.sh")
subprocess.call([smartgit_path])
  • malikarumi@Tetuoan2:~$ python smartgit.py Traceback (most recent call last): File "smartgit.py", line 16, in <module> smartgit_path = os.expanduser("~/Download/smartgit/bin/smartgit.sh") AttributeError: 'module' object has no attribute 'expanduser' – Malik A. Rumi Sep 12 '17 at 19:34
  • @MalikA.Rumi oh, yeah, typo. expanduser and related functions are in os.path – muru Sep 13 '17 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.