Windows 上的 Python os.path.join
我正在尝试学习 python 并正在制作一个输出脚本的程序。我想使用 os.path.join,但我很困惑。根据 docs 如果我说:
os.path.join('c:', 'sourcedir')
我得到 "C:sourcedir “
。根据文档,这是正常的,对吧?
但是当我使用 copytree 命令时,Python 将以所需的方式输出它,例如:
import shutil
src = os.path.join('c:', 'src')
dst = os.path.join('c:', 'dst')
shutil.copytree(src, dst)
这是我得到的错误代码:
WindowsError: [Error 3] The system cannot find the path specified: 'C:src/*.*'
如果我用 os.path 包装 os.path.join .normpath 我得到同样的错误。
如果这个 os.path.join 不能以这种方式使用,那么我对其用途感到困惑。
根据 Stack Overflow 建议的页面,在连接中不应使用斜杠——我认为这是正确的?
I am trying to learn python and am making a program that will output a script. I want to use os.path.join, but am pretty confused. According to the docs if I say:
os.path.join('c:', 'sourcedir')
I get "C:sourcedir"
. According to the docs, this is normal, right?
But when I use the copytree command, Python will output it the desired way, for example:
import shutil
src = os.path.join('c:', 'src')
dst = os.path.join('c:', 'dst')
shutil.copytree(src, dst)
Here is the error code I get:
WindowsError: [Error 3] The system cannot find the path specified: 'C:src/*.*'
If I wrap the os.path.join
with os.path.normpath
I get the same error.
If this os.path.join
can't be used this way, then I am confused as to its purpose.
According to the pages suggested by Stack Overflow, slashes should not be used in join—that is correct, I assume?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
更迂腐的是,最与 python 文档一致的答案是:
因为你还需要 os.sep 作为 posix 根路径:
To be even more pedantic, the most python doc consistent answer would be:
Since you also need os.sep for the posix root path:
Windows 对每个驱动器有一个当前目录的概念。因此,
"c:sourcedir"
表示当前 C: 目录中的“sourcedir”,您需要指定一个绝对目录。其中任何一个都应该可以工作并给出相同的结果,但我目前没有启动 Windows 虚拟机来仔细检查:
Windows has a concept of current directory for each drive. Because of that,
"c:sourcedir"
means "sourcedir" inside the current C: directory, and you'll need to specify an absolute directory.Any of these should work and give the same result, but I don't have a Windows VM fired up at the moment to double check:
迂腐地说,将 / 或 \ 硬编码为路径分隔符可能不好。也许这会是最好的?
或者
To be pedantic, it's probably not good to hardcode either / or \ as the path separator. Maybe this would be best?
or
对于可在 Windows 和 Linux 上运行的与系统无关的解决方案,无论输入路径是什么,都可以使用
在 Windows 上:
在 Linux 上:
For a system-agnostic solution that works on both Windows and Linux, no matter what the input path, one could use
On Windows:
On Linux:
os.path.join('C:', 'src') 未按您预期工作的原因是您链接到的文档中的某些内容:
正如 Ghostdog 所说,您可能需要
mypath=os.path.join('c:\\', 'sourcedir')
The reason
os.path.join('C:', 'src')
is not working as you expect is because of something in the documentation that you linked to:As ghostdog said, you probably want
mypath=os.path.join('c:\\', 'sourcedir')
我想说这是一个(windows)python bug。
为什么会出现错误?
我认为这个说法应该是
True
,但在Windows机器上它是
False
。I'd say this is a (windows)python bug.
Why bug?
I think this statement should be
True
But it is
False
on windows machines.要加入 Windows 路径,
基本上尝试一下,您需要转义斜杠
to join a windows path, try
basically, you will need to escape the slash
您有几种可能的方法来处理 Windows 上的路径,从最硬编码的方法(如使用原始字符串文字或转义反斜杠)到最少的方法。下面是一些可以按预期工作的示例。使用更适合您需求的东西。
You have a few possible approaches to treat path on Windows, from the most hardcoded ones (as using raw string literals or escaping backslashes) to the least ones. Here follows a few examples that will work as expected. Use what better fits your needs.
同意@georg-
我会说为什么我们需要蹩脚的
os.path.join
-更好地使用str.join
或unicode.join
例如Consent with @georg-
I would say then why we need lame
os.path.join
- better to usestr.join
orunicode.join
e.g.回答您的评论:“其他'//''c:','c:\\'不起作用(C:\\创建了两个反斜杠,C:\根本不起作用)”< /em>
在 Windows 上使用
os.path.join('c:', 'sourcedir')
会自动在 sourcedir 前面添加两个反斜杠
\\
。要解析路径,因为 python 在 Windows 上也可以使用正斜杠 -> '/',只需将
.replace('\\','/')
添加到os.path 即可。加入
如下:-os.path.join('c:\\', 'sourcedir').replace('\\','/')
例如:
os.path.join('c:\\', 'temp').replace('\\','/')
输出:'C:/temp'
answering to your comment : "the others '//' 'c:', 'c:\\' did not work (C:\\ created two backslashes, C:\ didn't work at all)"
On windows using
os.path.join('c:', 'sourcedir')
will automatically add two backslashes
\\
in front of sourcedir.To resolve the path, as python works on windows also with forward slashes -> '/', simply add
.replace('\\','/')
withos.path.join
as below:-os.path.join('c:\\', 'sourcedir').replace('\\','/')
e.g:
os.path.join('c:\\', 'temp').replace('\\','/')
output : 'C:/temp'
所提出的解决方案很有趣并且提供了很好的参考,但是它们只是部分令人满意。当您有一个特定情况或您知道输入字符串的格式时,可以手动添加分隔符,但在某些情况下您可能希望以编程方式对通用输入执行此操作。
经过一些实验,我认为标准是,如果第一段是驱动器号,则不添加路径分隔符,这意味着单个字母后跟冒号,无论它是否对应于实际单位。
例如:
测试标准并应用路径校正的一种便捷方法是使用 os.path.splitdrive 将第一个返回的元素与测试值进行比较,例如 t+os.path.sep if os.path.splitdrive(t)[0]==t else t。
测试:
它可能可以改进为对于尾随空格更加健壮,我只在 Windows 上测试了它,但我希望它能提供一个想法。
另请参阅 Os.path :你能解释一下这种行为吗? 有趣的Windows 以外的系统的详细信息。
The proposed solutions are interesting and offer a good reference, however they are only partially satisfying. It is ok to manually add the separator when you have a single specific case or you know the format of the input string, but there can be cases where you want to do it programmatically on generic inputs.
With a bit of experimenting, I believe the criteria is that the path delimiter is not added if the first segment is a drive letter, meaning a single letter followed by a colon, no matter if it corresponds to a real unit.
For example:
A convenient way to test for the criteria and apply a path correction can be to use
os.path.splitdrive
comparing the first returned element to the test value, liket+os.path.sep if os.path.splitdrive(t)[0]==t else t
.Test:
it can be probably be improved to be more robust for trailing spaces, and I have tested it only on windows, but I hope it gives an idea.
See also Os.path : can you explain this behavior? for interesting details on systems other then windows.
我通过使用以下方法解决了这个问题:
join
这里不是来自os.path.join()
,而是来自''.join()
它在以下情况下很有用:
I got around this by using:
join
here is not fromos.path.join()
, but from''.join()
It can be useful in the following situation:
为了简单起见,解决方法:
For simplicity's sake, a workaround:
在 Windows 中使用 os.paht.join("/", "Temp") 默认情况下会导致 /Temp,但听起来很奇怪,使用该路径作为完整路径没有问题相当于“C:/Temp”,它适用于保存文件和打开文件。
In windows using
os.paht.join("/", "Temp")
will result in /Temp by default but as strange as it sounds, there is no problem in using that path as a full path equivalent to "C:/Temp" and it works for both saving files and opening files.