nKife
nKife

Reputation: 3

Changing dir to execute program in linux

I am trying to execute a program from a directory

import os
os.chdir("/home/user/a/b")
with cd("/home/user/a/b"):
    run ("./program")

i get cd is not defined... any help appreciated cheers

Upvotes: 0

Views: 75

Answers (2)

Rahul Tanwani
Rahul Tanwani

Reputation: 441

It looks like you are trying to use functionalities of fabric. Make sure fabric is installed, and cd and run are imported from fabric. Something like,

from fabric.context_managers import cd
from fabric.operations import run
import os
os.chdir("/home/user/a/b")
with cd("/home/user/a/b"):
    run ("./program")

Save your file as fabfile.py,and from the same directory run it as:

fab -H localhost

For more information on the fabric, checkout: fabric

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993183

I'm not sure what instructions you're following to get what you showed. There is no built-in function called cd or run in Python.

You can call a program in a specific directory using the subprocess module:

import subprocess

subprocess.call("./program", cwd="/home/user/a/b")

The cwd argument causes the call function to automatically switch to that directory before starting the program named in the first argument.

Upvotes: 1

Related Questions