1

作为 python 的新手,我想我会编写一个小 python3 脚本来帮助我在命令行上切换目录(ubuntu 可信赖)。不幸os.chdir()的是似乎不起作用。我尝试以各种方式对其进行修补,例如在路径周围放置引号,删除前导斜杠(这显然不起作用)甚至只是对其进行硬编码,但我无法让它工作 - 任何人都可以告诉我我在这里缺少什么?

调用chdir()发生在最后 - 你也可以在github中看到代码

#!/usr/bin/env python3
# @python3
# @author sabot <sabot@inuits.eu>
"""Switch directories without wearing out your slash key"""
import sys
import os
import json
import click

__VERSION__ = '0.0.1'

# 3 params are needed for click callback
def show_version(ctx, param, value):
    """Print version information and exit."""
    if not value:
        return
    click.echo('Goto %s' % __VERSION__)
    ctx.exit() # quit the program

def add_entry(dictionary, filepath, path, alias):
    """Add a new path alias."""
    print("Adding alias {} for path {} ".format(alias,path))
    dictionary[alias] = path

    try:
        jsondata = json.dumps(dictionary, sort_keys=True)
        fd = open(filepath, 'w')
        fd.write(jsondata)
        fd.close()
    except Exception as e:
        print('Error writing to dictionary file: ', str(e))
        pass

def get_entries(filename):
    """Get the alias entries in json."""
    returndata = {}
    if os.path.exists(filename) and os.path.getsize(filename) > 0:
        try:
            fd = open(filename, 'r')
            entries = fd.read()
            fd.close()
            returndata = json.loads(entries)

        except Exception as e:
            print('Error reading dictionary file: ', str(e))
            pass
    else:
        print('Dictionary file not found or empty- spawning new one in', filename)
        newfile = open(filename,'w')
        newfile.write('')
        newfile.close()

    return returndata

@click.command()
@click.option('--version', '-v', is_flag=True, is_eager=True,
              help='Print version information and exit.', expose_value=False,
              callback=show_version)
@click.option('--add', '-a', help="Add a new path alias")
@click.option('--target', '-t', help="Alias target path instead of the current directory")
@click.argument('alias', default='currentdir')
@click.pass_context
def goto(ctx, add, alias, target):
    '''Go to any directory in your filesystem''' 

    # load dictionary
    filepath = os.path.join(os.getenv('HOME'), '.g2dict')
    dictionary = get_entries(filepath)

    # add a path alias to the dictionary
    if add:
        if target: # don't use current dir as target
            if not os.path.exists(target):
                print('Target path not found!')
                ctx.exit()
            else:
                add_entry(dictionary, filepath, target, add)
        else: # use current dir as target
            current_dir = os.getcwd()
            add_entry(dictionary, filepath, current_dir, add)

    elif alias != 'currentdir':
        if alias in dictionary:
            entry = dictionary[alias]
            print('jumping to',entry)
            os.chdir(entry)
        elif alias == 'hell':
            print("Could not locate C:\Documents and settings")
        else:
            print("Alias not found in dictionary - did you forget to add it?")

if __name__ == '__main__':
    goto()
4

2 回答 2

1

问题不在于 Python,问题在于您尝试做的事情是不可能的。

当您启动 Python 解释器(脚本或交互式 REPL)时,您可以从“shell”(Bash 等)中执行此操作。shell 有一些工作目录,它在同一个目录中启动 Python。当 Python 更改自己的工作目录时,它不会影响父 shell,也不会影响启动后的 shell 工作目录的更改。

如果你想编写一个程序来改变你的 shell 中的目录,你应该在你的 shell 本身中定义一个函数。该函数可以调用 Python 来确定要切换到的目录,例如,shell 函数可以简单地cd $(~/myscript.py)打印myscript.py它想要切换到的目录。

于 2015-01-20T01:39:04.900 回答
0

这是@ephemient 的 C 解决方案的 Python 3 版本:

#!/usr/bin/env python3
"""Change parent working directory."""
#XXX DIRTY HACK, DO NOT DO IT
import os
import sys
from subprocess import Popen, PIPE, DEVNULL, STDOUT

gdb_cmd = 'call chdir("{dir}")\ndetach\nquit\n'.format(dir=sys.argv[1])
with Popen(["gdb", "-p", str(os.getppid()), '-q'],
           stdin=PIPE, stdout=DEVNULL, stderr=STDOUT) as p:
    p.communicate(os.fsencode(gdb_cmd))
sys.exit(p.wait())

例子:

# python3 cd.py /usr/lib && python3 -c 'import os; print(os.getcwd())'
于 2015-01-21T15:12:47.747 回答