因此,我有一个python文件sample.py,它位于“ A / B / C”位置,而我当前的工作目录为“ D / E”。现在在sample.py中,我需要的目录是'A / B / C'。谁能帮我这个。我已经尝试过"dir_path = os.path.dirname(os.path.realpath('__sample__'))"
,但是它返回了当前的工作目录。
答案 0 :(得分:0)
更改行:
dir_path = os.path.dirname(os.path.realpath('__sample__'))
到
dir_path = os.path.dirname(os.path.realpath(__file__))
这应该为您工作。查找有关__file__
here的更多信息。