Search code examples
pythonpython-2.7terminalfabric

How do I permanently change directory using Fabric?


Here's my code. The idea is to switch into the directory "Aquantia" which I have a symlink too, echo a string into a text file, then redirect the output of a Hello World program into the text file. If I run the fabfile.py in the actual Aquantia directory it works, but when I launch from Home, it fails.

from fabric.api import local, settings, abort, run, cd

def run():
    with cd('~/Aquantia'):
        local("echo 'love' > test.txt ")
        local("less test.txt")
        local("./test>test.txt")
        local("less test.txt")

Here's the error message:

DN0a152ee4:~ bmassoumi$ fab run
[localhost] local: echo 'love' > test.txt 
[localhost] local: less test.txt
[localhost] local: ./test>test.txt
/bin/sh: ./test: No such file or directory

Fatal error: local() encountered an error (return code 127) while executing './test>test.txt'

Aborting.
local() encountered an error (return code 127) while executing './test>test.txt'

Solution

  • From the fabric documentation:

    cd only affects remote paths – to modify local paths, use lcd.