【问题标题】:Concatenating file paths while overriding window file name length problem覆盖窗口文件名长度问题时连接文件路径
【发布时间】: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

【问题讨论】:

标签: python pandas concatenation glob


【解决方案1】:

所以最终我得到这个工作的方式是使用映射的文件路径。我有我正在访问的网络驱动器映射到我的 Z 驱动器。我使用此代码访问它。它工作正常,只是我必须确保在运行代码之前先访问了驱动器。

location = "\\\\?\\Z:" +str(file_path)+ "*\*\*\*\**\*"+ str(log1) +"*.pdf"

所以我已经从\\server\folder1\folder2\foldere3.. 移动到Z:\foldere2\foldere3.. 所以当连接在一起时\\\\?\\Z:\folder2\folder3..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多