python - 如何获取另一个文件夹的父文件夹?

标签 python windows django linux path

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

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__))D:\Projects\Test\src\test in file_path 我将拥有 D:\Projects\Test\src\webapp.cfg。这在 Ubuntu 8.04 下不起作用。

编辑 1: 我在虚拟机上运行 Ubuntu 8.04。我在上面安装了 Python 2.5.2 版本。

最佳答案

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

关于python - 如何获取另一个文件夹的父文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4875937/

相关文章:

c# - 以编程方式重新定位 VLC 窗口

django - 使用Django获取每个月的总数

html - django 将 html href 和 src 替换为 {% static %} 标签

python socket.error : [Errno 9] Bad file descriptor

Python在线抓取数据,但csv文件未显示正确的数据格式

python - 另一个类中的 tkinter 变量

css - Django 只应用我的 css 文件中的第一个样式

python - 如何开始在opencv中识别物体

windows - 连接的套接字信息

windows - 如何使用 Windows API 在任何时区之间转换 utc 日期时间?