为什么Python不使用工作目录以及如何修复它?

2024-04-27 08:11:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个问题,要花很长时间来解释,所以我要把它分解成有趣的部分。我试着用kconfiglib.py公司. 这是我的代码(of config.in

[intro_code]
source "b.in"

我的代码在[简介代码]中做它应该做的事情。我不确定source是否是一个python关键字,但它是关于输入/包含一个不同的文件(这里的文件名为b.in,与执行的脚本位于同一个文件夹中)

错误消息:现在运行脚本时,出现以下错误消息:

IOError: config.in:51: sourced file "b.in" (expands to
"b.in") not found. Perhaps base_dir
(argument to Config.__init__(), currently
"$srctree") is set to the wrong value.

我要解决的问题:我尝试将工作目录更改为Sourcecodes(即config.inb.in所在的位置)位于config.in之上:

os.chdir("/home/fedor/Sourcecodes") 
retval = os.getcwd()
print "Directory changed successfully %s" % retval

在执行过程中,它返回:

Directory changed successfully /home/fedor/Sourcecodes

因此,目录似乎是正常的,但同样的错误消息出现无论如何。你知道吗

它在时工作:使用到b.in(比如source "/home/fedor/Sourcecodes/b.in"))的绝对路径,它就工作了,但我不能这样使用脚本。你知道吗

有人知道如何让python与执行的脚本在同一个目录中查找吗?你知道吗

[编辑:]如您所愿,完整代码:

我叫python /home/fedor/Sourcecodes/Kconfiglib/examples/print_tree.py /home/fedor/Sourcecodes/config.in

kconfiglib示例中的print_tree.py

# Prints a tree of all items in the configuration

import kconfiglib
import sys
import os

os.chdir("/home/fedor/BR1311") 


retval = os.getcwd()

print "Directory changed successfully %s" % retval

def print_with_indent(s, indent):
    print (" " * indent) + s

def print_items(items, indent):
    for item in items:
        if item.is_symbol():
            print_with_indent("config {0}".format(item.get_name()), indent)
        elif item.is_menu():
            print_with_indent('menu "{0}"'.format(item.get_title()), indent)
            print_items(item.get_items(), indent + 2)
        elif item.is_choice():
            print_with_indent('choice', indent)
            print_items(item.get_items(), indent + 2)
        elif item.is_comment():
            print_with_indent('comment "{0}"'.format(item.get_text()), indent)

conf = kconfiglib.Config(sys.argv[1])
print_items(conf.get_top_level_items(), 0)

config.in

menu "Audio and video applications"
config BR2_PACKAGE_WIPE
    bool "wipe"
    help
      Wipe is a little command for securely erasing files
      from magnetic media. It compiles under various unix platforms.

      http://wipe.sourceforge.net

config BR2_PACKAGE_BONNIE
    bool "bonnie++"
    depends on BR2_INSTALL_LIBSTDCPP
    depends on BR2_USE_MMU # fork()
    help
      Filesystem tester

      http://www.coker.com.au/bonnie++/

comment "bonnie++ needs a toolchain w/ C++"
    depends on BR2_USE_MMU
    depends on !BR2_INSTALL_LIBSTDCPP
endmenu

source "b.in"

以及b.in(与config.in完全相同,但最后缺少source命令):

menu "Audio and video applications"
config BR2_PACKAGE_WIPE
    bool "wipe"
    help
      Wipe is a little command for securely erasing files
      from magnetic media. It compiles under various unix platforms.

      http://wipe.sourceforge.net

config BR2_PACKAGE_BONNIE
    bool "bonnie++"
    depends on BR2_INSTALL_LIBSTDCPP
    depends on BR2_USE_MMU # fork()
    help
      Filesystem tester

      http://www.coker.com.au/bonnie++/

comment "bonnie++ needs a toolchain w/ C++"
    depends on BR2_USE_MMU
    depends on !BR2_INSTALL_LIBSTDCPP
endmenu

Tags: inconfighomegetisonitemsitem
1条回答
网友
1楼 · 发布于 2024-04-27 08:11:19

答案就在您发布的错误消息中:

Perhaps base_dir (argument to Config.__init__(), currently "$srctree") 
is set to the wrong value.

查看github上的source__init__类的Config使用多个参数,所有参数都带有默认值。你知道吗

def __init__(self,
             filename = "Kconfig",
             base_dir = "$srctree",
             print_warnings = True,
             print_undef_assign = False):

…在类的docstring中,base_dir参数解释如下:

base_dir (default: "$srctree")   The base directory relative to which
'source' statements within Kconfig files will work. For the
Linux kernel this should be the top-level directory of the
kernel tree. $-references to environment variables will be
expanded.

The environment variable 'srctree' is set by the Linux makefiles
to the top-level kernel directory. A default of "." would not
work if an alternative build directory is used.

我怀疑如果你把'/home/fedor/BR1311'传给这个__init__,而不是改成它,比如:

conf = kconfiglib.Config(sys.argv[1], '/home/fedor/BR1311')

事情会好得多。你知道吗

相关问题 更多 >