使用 pywin32 客户端访问映射网络驱动器上的文件时出现问题
Having problems with accessing files on a mapped network drive, using pywin32 client
我目前正在使用以下代码打开一个word文档(然后保存,但目前与打开文件无关):
word=win32.Dispatch('Word.Application')
try:
doc = word.Documents.Open('S:\problem\file.docx')
except Exception as e:
print(e)
(-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'Sorry, we
couldn’t find your file. Is it possible it was moved, renamed or
deleted?\r (S:\problem\file.docx)',
'wdmain11.chm', 24654, -2146823114), None)
"problem" 目录是 win32 客户端似乎无法识别的唯一目录。我已经多次将它重命名为单个字母以查看命名是否出于某种原因的问题,但这似乎不是问题所在。
文件路径也被docx函数识别- docx.Document并且能够读取目录中的文件。以下是 docx 片段的相同代码和结果:
Document('S://problem/file.docx')
<docx.document.Document at 0x83d3c18>
在Python字符串中,bkslash ("\")是具有特殊含义的字符之一:它用于创建转义序列(特殊chars)连同它后面的字符(这来自 C)。 [Python 3]: String and Bytes literals 声明如下:
The backslash (\
) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
在你的字符串中,你有 "\p"(即 OK)和 "\f" 被解释为 single char (form feed - new page),使您的路径无效。
为了解决这个问题,可以:
- 转义(双)字符串中的任何 "\"(好吧,这只是一种预防措施,因为您只需要转义产生转义序列的那些 - 在我们的示例 "\p" 非常好),除了那些您 想要 产生转义序列的示例:'S:\problem\file.docx'
- 通过在其前面添加 r 标记来使字符串 raw(请注意,如果字符串以 "\",那还是要转义的,否则会转义字符串结束标记(' or ") 紧随其后,产生 SyntaxError): r'S:\problem\file.docx'
作为一般规则,为了确保字符串如您所想,可以:
- 检查它们的长度:如果它小于你看到的字符数(在代码中),这意味着至少有一个转义序列
- 使用repr
示例:
>>> import sys
>>> sys.version
'3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)]'
>>>
>>> s0 = 'S:\problem\file.docx'
>>> s1 = 'S:\problem\file.docx'
>>> s2 = r'S:\problem\file.docx'
>>>
>>> len(s0), len(s1), len(s2)
(19, 20, 20)
>>>
>>> s0 == s1, s1 == s2
(False, True)
>>>
>>> repr(s0), repr(s1), repr(s2)
("'S:\\problem\x0cile.docx'", "'S:\\problem\\file.docx'", "'S:\\problem\\file.docx'")
我目前正在使用以下代码打开一个word文档(然后保存,但目前与打开文件无关):
word=win32.Dispatch('Word.Application')
try:
doc = word.Documents.Open('S:\problem\file.docx')
except Exception as e:
print(e)
(-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'Sorry, we
couldn’t find your file. Is it possible it was moved, renamed or
deleted?\r (S:\problem\file.docx)',
'wdmain11.chm', 24654, -2146823114), None)
"problem" 目录是 win32 客户端似乎无法识别的唯一目录。我已经多次将它重命名为单个字母以查看命名是否出于某种原因的问题,但这似乎不是问题所在。
文件路径也被docx函数识别- docx.Document并且能够读取目录中的文件。以下是 docx 片段的相同代码和结果:
Document('S://problem/file.docx')
<docx.document.Document at 0x83d3c18>
在Python字符串中,bkslash ("\")是具有特殊含义的字符之一:它用于创建转义序列(特殊chars)连同它后面的字符(这来自 C)。 [Python 3]: String and Bytes literals 声明如下:
The backslash (
\
) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
在你的字符串中,你有 "\p"(即 OK)和 "\f" 被解释为 single char (form feed - new page),使您的路径无效。
为了解决这个问题,可以:
- 转义(双)字符串中的任何 "\"(好吧,这只是一种预防措施,因为您只需要转义产生转义序列的那些 - 在我们的示例 "\p" 非常好),除了那些您 想要 产生转义序列的示例:'S:\problem\file.docx'
- 通过在其前面添加 r 标记来使字符串 raw(请注意,如果字符串以 "\",那还是要转义的,否则会转义字符串结束标记(' or ") 紧随其后,产生 SyntaxError): r'S:\problem\file.docx'
作为一般规则,为了确保字符串如您所想,可以:
- 检查它们的长度:如果它小于你看到的字符数(在代码中),这意味着至少有一个转义序列
- 使用repr
示例:
>>> import sys >>> sys.version '3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)]' >>> >>> s0 = 'S:\problem\file.docx' >>> s1 = 'S:\problem\file.docx' >>> s2 = r'S:\problem\file.docx' >>> >>> len(s0), len(s1), len(s2) (19, 20, 20) >>> >>> s0 == s1, s1 == s2 (False, True) >>> >>> repr(s0), repr(s1), repr(s2) ("'S:\\problem\x0cile.docx'", "'S:\\problem\\file.docx'", "'S:\\problem\\file.docx'")