【发布时间】:2020-02-12 15:45:49
【问题描述】:
我在使用 python 3.7 连接服务器目录路径时遇到问题。我有它的工作,但需要引入此代码(如 here 所讨论的),以便覆盖 Windows 导演路径上的最大 260 个字符限制。
\\\\?\\UNC
似乎无法获得正确的语法。在未添加 UNC 的情况下,以下功能正在运行(带回匹配项)。但是当它遇到一个长文件名时,它会使程序崩溃。
def file_path(data):
latest_file_list = []
for index, row in data.iterrows():
log1 = row['Log number 2001']
file_path = row['file_path_short']
location = r"" +str(file_path)+ "*\*\*\*\**\*"+ str(log1) +"*.pdf"
# check file on drive
list_of_files = glob.glob(location, recursive=True)
位置等于:
\\servername\...
下面的代码是我正在测试修复字符限制的代码,它不会给出错误但也不会返回任何匹配项。
location2 = r"\\\\?\\UNC" +str(file_path)+ "*\*\*\*\**\**\*"+ str(log1) +"*.pdf"
location2 等于
\\?\UNC\servername\...
我试过用 u 和 r 作为前缀,但这没有帮助。
谁能给点指导?有更好/更简单的解决方案吗?
编辑
我也尝试过这种方法,它适用于短文件名,但长文件名会出现 [WinError 3] 错误。
import os
location3 = os.path.join(r"\\\\?\\UNC", str(file_path), "*\*\**", "*"+ str(log1) +"*" +".pdf" )
编辑2
所以我一直在使用以下代码玩 pathlib:
from pathlib import Path
location4 = Path(r"\\\\?\\UNC", str(file_path), "*\*\**", "*"+ str(log1) +"*" +".pdf" )
但我目前收到此错误TypeError: expected string or bytes-like object
【问题讨论】:
-
你尝试过使用 Python3 的内置路径库吗? docs.python.org/3/library/pathlib.html
-
@Hrabal 我尝试过 os.path.join 但似乎无法正常工作。
标签: python pandas concatenation glob