【发布时间】:2014-05-20 11:03:51
【问题描述】:
我有一个问题,需要很长时间才能解释,所以我将把它分解成有趣的部分。我尝试使用 kconfiglib.py 解析一些 kconfig。这是我的代码(config.in)
[intro_code]
source "b.in"
我的代码完全按照 [intro_code] 的方式进行。我不确定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.in 和b.in 所在的位置)在config.in 之上:
os.chdir("/home/fedor/Sourcecodes")
retval = os.getcwd()
print "Directory changed successfully %s" % retval
在执行期间它返回:
目录更改成功 /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-examples 的 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
【问题讨论】:
-
请发布完整代码。
-
这完全取决于
kconfiglib.py做了什么。它似乎有一个您忽略的基本目录参数。 -
我在顶帖末尾添加了代码
标签: python linux import python-import