【问题标题】:How can I obtain the parent folder of another folder?如何获取另一个文件夹的父文件夹?
【发布时间】:2011-06-20 01:01:27
【问题描述】:

在我的设置文件中,我有以下几行:

import os
import sys

PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))
file_path = os.path.join(os.path.realpath(os.path.dirname(__file__)),"..",  "webapp.cfg")

在 Windows 下,如果 os.path.realpath(os.path.dirname(__file__))file_path 中是 D:\Projects\Test\src\test,我将拥有 D:\Projects\Test\src\webapp.cfg。这在 Ubuntu 8.04 下不起作用。

EDIT1:我在虚拟机上运行 Ubuntu 8.04。我已经安装了 Python 2.5.2 版本。

【问题讨论】:

  • “不起作用”是什么意思?你的电脑会爆炸吗?你在file_path 中得到“粉色独角兽”吗?它对我有用。
  • python的版本不一样吗? 8.04 是相当旧的 Ubuntu 版本,因此默认的 python 安装将是相同的年龄。
  • 它说没有这样的文件或目录:'/home/Projects/Test/src/test/../webapp.cfg
  • @Seitaridis:嗯,有这样的文件吗?
  • 您可能需要考虑添加 file_path = os.path.normpath(file_path) 以消除上层引用 (/../)。不过,我怀疑这会解决您的问题。因此,评论而不是答案。 :)

标签: python windows django linux path


【解决方案1】:

添加file_path = os.path.normpath(file_path) 以消除上层引用 (/../)。

【讨论】:

    【解决方案2】:
    parent = os.path.normpath(os.path.join(directory, ".."))
    

    【讨论】:

      猜你喜欢
      • 2018-12-14
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多