xxmbabanexx
xxmbabanexx

Reputation: 8706

How to use cd terminal command in Python

Question

How can I change the active directory on the raspberry pi using cd and the subprocess module?

Background

Since I absolutely hate to use the command line, I am trying to create a basic GUI text-editor which can also compile my programs. For now, I am just trying to change the directory to Desktop. To do this, I am using the subprocess module. Here is my current code:

from subprocess import *
call(["cd","Desktop"])

In the terminal, this line (cd Desktop) would change the active directory to Desktop. Oddly, when I run it through subprocess, I am given this error:

OSError: [Errno 2] No such file or directory

Tech Specs

Raspberry Pi Model B

Raspbian "Wheezy" OS

Upvotes: 1

Views: 5951

Answers (2)

Jonathan Vanasco
Jonathan Vanasco

Reputation: 15680

i don't mean to derail the original question, but if you're trying to automate a lot of tasks, you can use the fabric module.

it has a rather simple syntax like this:

with cd('/path/to/app'):
    with prefix('workon myvenv'):
        run('./manage.py syncdb')

http://docs.fabfile.org/en/1.6/api/core/context_managers.html

it's designed for remote usage over ssh, but many people use it for a lot of local management & deployment

the lcd command works on your local machine:

with lcd('/path/to/app'):
    with prefix('workon myvenv'):
        run('./manage.py syncdb')

Upvotes: 1

Satish
Satish

Reputation: 17467

I would try os.chdir

import os
os.chdir("/path/to/dir")

Upvotes: 5

Related Questions