标签归档:path-manipulation

Windows上的Python os.path.join

问题:Windows上的Python os.path.join

我正在尝试学习python,正在制作一个将输出脚本的程序。我想使用os.path.join,但是很困惑。根据文档,如果我说:

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:[错误3]系统找不到指定的路径:'C:src /*.*'

如果我用换行os.path.joinos.path.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?


回答 0

Windows具有每个驱动器的当前目录的概念。因此,"c:sourcedir"意味着在当前C:目录中有“ sourcedir”,并且您需要指定一个绝对目录。

这些中的任何一个都应该工作并给出相同的结果,但是目前我还没有启动Windows VM进行仔细检查:

"c:/sourcedir"
os.path.join("/", "c:", "sourcedir")
os.path.join("c:/", "sourcedir")

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:

"c:/sourcedir"
os.path.join("/", "c:", "sourcedir")
os.path.join("c:/", "sourcedir")

回答 1

要进一步讲究知识,最符合python doc的答案是:

mypath = os.path.join('c:', os.sep, 'sourcedir')

由于您还需要os.sep作为posix根路径:

mypath = os.path.join(os.sep, 'usr', 'lib')

To be even more pedantic, the most python doc consistent answer would be:

mypath = os.path.join('c:', os.sep, 'sourcedir')

Since you also need os.sep for the posix root path:

mypath = os.path.join(os.sep, 'usr', 'lib')

回答 2

之所以os.path.join('C:', 'src')无法正常工作,是因为您链接到了文档中的某些内容:

请注意,在Windows上,由于每个驱动器都有一个当前目录,所以os.path.join(“ c:”,“ foo”)表示相对于驱动器C:(c:foo)上当前目录的路径,而不是c :\ foo

正如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:

Note that on Windows, since there is a current directory for each drive, os.path.join(“c:”, “foo”) represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

As ghostdog said, you probably want mypath=os.path.join('c:\\', 'sourcedir')


回答 3

对于在Windows和Linux上均可使用的与系统无关的解决方案,无论输入路径如何,都可以使用 os.path.join(os.sep, rootdir + os.sep, targetdir)

在窗口上:

>>> os.path.join(os.sep, "C:" + os.sep, "Windows")
'C:\\Windows'

在Linux上:

>>> os.path.join(os.sep, "usr" + os.sep, "lib")
'/usr/lib'

For a system-agnostic solution that works on both Windows and Linux, no matter what the input path, one could use os.path.join(os.sep, rootdir + os.sep, targetdir)

On WIndows:

>>> os.path.join(os.sep, "C:" + os.sep, "Windows")
'C:\\Windows'

On Linux:

>>> os.path.join(os.sep, "usr" + os.sep, "lib")
'/usr/lib'

回答 4

要学究,将/或\硬编码为路径分隔符可能不好。也许这是最好的?

mypath = os.path.join('c:%s' % os.sep, 'sourcedir')

要么

mypath = os.path.join('c:' + os.sep, 'sourcedir')

To be pedantic, it’s probably not good to hardcode either / or \ as the path separator. Maybe this would be best?

mypath = os.path.join('c:%s' % os.sep, 'sourcedir')

or

mypath = os.path.join('c:' + os.sep, 'sourcedir')

回答 5

我会说这是一个(windows)python错误。

为什么要臭虫?

我认为这句话应该是 True

os.path.join(*os.path.dirname(os.path.abspath(__file__)).split(os.path.sep))==os.path.dirname(os.path.abspath(__file__))

但这是False在Windows机器上。

I’d say this is a (windows)python bug.

Why bug?

I think this statement should be True

os.path.join(*os.path.dirname(os.path.abspath(__file__)).split(os.path.sep))==os.path.dirname(os.path.abspath(__file__))

But it is False on windows machines.


回答 6

加入Windows路径,请尝试

mypath=os.path.join('c:\\', 'sourcedir')

基本上,您将需要逃脱斜线

to join a windows path, try

mypath=os.path.join('c:\\', 'sourcedir')

basically, you will need to escape the slash


回答 7

您可以使用几种可能的方法来处理Windows上的路径,从最硬编码的方法(如使用原始字符串文字或转义反斜杠)到最少的方法。以下是一些可以按预期工作的示例。使用更适合您需求的产品。

In[1]: from os.path import join, isdir

In[2]: from os import sep

In[3]: isdir(join("c:", "\\", "Users"))
Out[3]: True

In[4]: isdir(join("c:", "/", "Users"))
Out[4]: True

In[5]: isdir(join("c:", sep, "Users"))
Out[5]: True

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.

In[1]: from os.path import join, isdir

In[2]: from os import sep

In[3]: isdir(join("c:", "\\", "Users"))
Out[3]: True

In[4]: isdir(join("c:", "/", "Users"))
Out[4]: True

In[5]: isdir(join("c:", sep, "Users"))
Out[5]: True

回答 8

同意@ georg-

我会说为什么我们需要la脚os.path.join-更好地使用str.joinunicode.join例如

sys.path.append('{0}'.join(os.path.dirname(__file__).split(os.path.sep)[0:-1]).format(os.path.sep))

Consent with @georg-

I would say then why we need lame os.path.join– better to use str.join or unicode.join e.g.

sys.path.append('{0}'.join(os.path.dirname(__file__).split(os.path.sep)[0:-1]).format(os.path.sep))

回答 9

回答您的评论:“其他’//”c:’,’c:\\’不起作用(C:\\创建了两个反斜杠,C:\根本不起作用)”

在Windows上,using os.path.join('c:', 'sourcedir') 将自动\\sourcedir前面添加两个反斜杠。

解析路径,因为python在Windows上也可以使用正斜杠-> “/”,只需.replace('\\','/')os.path.join如下: –

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('\\','/') with os.path.join as below:-

os.path.join('c:\\', 'sourcedir').replace('\\','/')

e.g: os.path.join('c:\\', 'temp').replace('\\','/')

output : ‘C:/temp’


回答 10

所提出的解决方案很有趣并且可以提供很好的参考,但是它们只是部分令人满意。当您遇到单个特定情况或知道输入字符串的格式时,可以手动添加分隔符,但是在某些情况下,您可能希望对通用输入进行编程编程。

经过一些试验,我相信标准是,如果第一个段是驱动器号(即单个字母后跟一个冒号),则不添加路径定界符,无论它是否对应于实际单位。

例如:

import os
testval = ['c:','c:\\','d:','j:','jr:','data:']

for t in testval:
    print ('test value: ',t,', join to "folder"',os.path.join(t,'folder'))
test value:  c: , join to "folder" c:folder
test value:  c:\ , join to "folder" c:\folder
test value:  d: , join to "folder" d:folder
test value:  j: , join to "folder" j:folder
test value:  jr: , join to "folder" jr:\folder
test value:  data: , join to "folder" data:\folder

测试标准并进行路径校正的一种便捷方法是,os.path.splitdrive将第一个返回的元素与测试值进行比较,例如t+os.path.sep if os.path.splitdrive(t)[0]==t else t

测试:

for t in testval:
    corrected = t+os.path.sep if os.path.splitdrive(t)[0]==t else t
    print ('original: %s\tcorrected: %s'%(t,corrected),' join corrected->',os.path.join(corrected,'folder'))
original: c:    corrected: c:\  join corrected-> c:\folder
original: c:\   corrected: c:\  join corrected-> c:\folder
original: d:    corrected: d:\  join corrected-> d:\folder
original: j:    corrected: j:\  join corrected-> j:\folder
original: jr:   corrected: jr:  join corrected-> jr:\folder
original: data: corrected: data:  join corrected-> data:\folder

可能可以对其进行改进以使其更适合尾随空间,并且我仅在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:

import os
testval = ['c:','c:\\','d:','j:','jr:','data:']

for t in testval:
    print ('test value: ',t,', join to "folder"',os.path.join(t,'folder'))
test value:  c: , join to "folder" c:folder
test value:  c:\ , join to "folder" c:\folder
test value:  d: , join to "folder" d:folder
test value:  j: , join to "folder" j:folder
test value:  jr: , join to "folder" jr:\folder
test value:  data: , join to "folder" data:\folder

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, like t+os.path.sep if os.path.splitdrive(t)[0]==t else t.

Test:

for t in testval:
    corrected = t+os.path.sep if os.path.splitdrive(t)[0]==t else t
    print ('original: %s\tcorrected: %s'%(t,corrected),' join corrected->',os.path.join(corrected,'folder'))
original: c:    corrected: c:\  join corrected-> c:\folder
original: c:\   corrected: c:\  join corrected-> c:\folder
original: d:    corrected: d:\  join corrected-> d:\folder
original: j:    corrected: j:\  join corrected-> j:\folder
original: jr:   corrected: jr:  join corrected-> jr:\folder
original: data: corrected: data:  join corrected-> data:\folder

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.