windows Python中的相对路径问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/319037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:39:28  来源:igfitidea点击:

Problem with relative path in Python

pythonwindows

提问by matt b

I know this is a simple, beginner-ish Python question, but I'm having trouble opening a file using a relative path. This behavior seems odd to me (coming from a non-Python background):

我知道这是一个简单的初学者级 Python 问题,但是我在使用相对路径打开文件时遇到了问题。这种行为对我来说似乎很奇怪(来自非 Python 背景):

import os, sys

titles_path = os.path.normpath("../downloads/movie_titles.txt")

print "Current working directory is {0}".format(os.getcwd())
print "Titles path is {0}, exists? {1}".format(movie_titles_path, os.path.exists(movie_titles_path))

titlesFile = open(movie_titles_path, 'r')
print titlesFile

This results in:

这导致:

C:\Users\Matt\Downloads\blah>testscript.py

Current working directory is C:\Users\Matt\Downloads\blah
Titles path is ..\downloads\movie_titles.txt, exists? False

Traceback (most recent call last):
  File "C:\Users\Matt\Downloads\blah\testscript.py", line 27, in <module>
    titlesFile = open(titles_path, 'r')
IOError: [Errno 2] No such file or directory: '..\downloads\movie_titles.txt'

However, a dir command shows this file at the relative path exists:

但是,dir 命令显示该文件在相对路径中存在:

C:\Users\Matt\Downloads\blah>dir /b ..\downloads\movie_titles.txt
movie_titles.txt

What's going on with how Python interprets relative paths on Windows? What is the correct way to open a file with a relative path?

Python 如何解释 Windows 上的相对路径?打开具有相对路径的文件的正确方法是什么?

Also, if I wrap my path in os.path.abspath(), then I get this output:

另外,如果我将路径包裹在 中os.path.abspath(),则会得到以下输出:

Current working directory is C:\Users\Matt\Downloads\blah
Titles path is C:\Users\Matt\Downloads\downloads\movie_titles.txt, exists? False
Traceback (most recent call last):
  File "C:\Users\Matt\Downloads\blah\testscript.py", line 27, in <module>
    titlesFile = open(titles_path, 'r')
IOError: [Errno 2] No such file or directory: 'C:\Users\Matt\Downloads\downloads\movie_titles.txt'

In this case, seems like the open()command is automatically escaping the \characters.

在这种情况下,似乎open()命令会自动转义\字符。

**Embarrassing final update: Looks like I munged a character in the pathanme :) The correct way to do this on Windows seems to be to use os.path.normpath(), as I did originally.

**令人尴尬的最终更新:看起来我在 pathanme 中添加了一个字符 :) 在 Windows 上执行此操作的正确方法似乎是使用os.path.normpath(),就像我最初所做的那样。

回答by Paul Fisher

normpathonly returns a normalized version of that particular path. It does not actually do the work of resolvingthe path for you. You might want to do os.path.abspath(yourpath).

normpath只返回该特定路径的规范化版本。它实际上并没有为您完成解决路径的工作。你可能想要做os.path.abspath(yourpath)

Also, I'm assuming you're on IronPython. Otherwise, the standard way of expressing that string format would be:

另外,我假设您使用的是 IronPython。否则,表达该字符串格式的标准方法是:

"Current working directory is %s" % os.getcwd()
"Titles path is %s, exists? %s" % (movie_titles_path, os.path.exists(movie_titles_path))

(Sorry, this is only an answer to the halfway posted question. I'm puzzled about the full solution.)

(抱歉,这只是对中途问题的回答。我对完整的解决方案感到困惑。)