如何在网络路径上正确使用os.listdir?

时间:2016-04-27 12:55:14

标签: python windows

以下代码:

>> sf

     Linear model Poly32:
     sf(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + 
                    p21*x^2*y + p12*x*y^2
       where x is normalized by mean -0.3736 and std 1.887
       and where y is normalized by mean -0.04893 and std 1.644
     Coefficients (with 95% confidence bounds):
       p00 =      0.4227  (-0.3731, 1.218)
       p10 =       1.764  (0.5627, 2.965)
       p01 =       1.313  (0.7715, 1.855)
       p20 =     -0.1054  (-0.6496, 0.4389)
       p11 =      0.4627  (0.03944, 0.8859)
       p02 =      0.1898  (-0.2443, 0.6239)
       p30 =     -0.6345  (-1.247, -0.02209)
       p21 =     -0.8263  (-1.32, -0.3327)
       p12 =     -0.4908  (-1.011, 0.02911)

>> sf_struct=struct(sf)
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or
DISPLAY to see the visible public details of an object. See 'help struct' for more information. 

    sf_struct = 

                 version: 2
            fCoeffValues: {[0.4227]  [1.7639]  [1.3130]  [-0.1054]  [0.4627]  [0.1898]  [-0.6345]  [-0.8263]  [-0.4908]}
             fProbValues: {1x0 cell}
                     sse: 59.5574
                     dfe: 40
                    rinv: [9x9 double]
            activebounds: [9x1 logical]
                   meanx: -0.3736
                   meany: -0.0489
                    stdx: 1.8875
                    stdy: 1.6441
                    xlim: [-2.8236 2.8090]
                    ylim: [-2.7585 2.6763]
                   fType: 'poly32'
               fTypename: 'Poly32'
               fCategory: 'library'
                    defn: 'p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + p21*x^2*y + p12*x*y^2'
                  fFeval: 1
                    expr: @polySurface
                   Adefn: {}
                   Aexpr: {}
                  linear: 1
                 derexpr: @polySurfaceDerivative
                 intexpr: []
                    args: [11x3 char]
                 isEmpty: 0
                 numArgs: 11
               numCoeffs: 9
             assignCoeff: [1x234 char]
              assignData: ' x = FITTYPE_INPUTS_{10}; y = FITTYPE_INPUTS_{11};'
              assignProb: ''
                   indep: [2x1 char]
                   depen: 'z'
                   coeff: [9x3 char]
                    prob: ''
              fConstants: {[3]  [2]}
        fNonlinearcoeffs: []
             fFitoptions: [1x1 curvefit.llsqoptions]
                fStartpt: []

>> [sf_struct.meanx, sf_struct.meany, sf_struct.stdx, sf_struct.stdy]

    ans =

       -0.3736   -0.0489    1.8875    1.6441

这样运行正常,但我试图让def tema_get_file(): logdir='T:\\' logfiles = sorted([ f for f in os.listdir(logdir) if f.startswith('tms_int_calls-')]) return logfiles[-1] 以直接路径运行: logdir 驱动器T是映射驱动器。最初,文件位于C盘上。

一旦我这样做,我收到错误消息:

  

WindowsError:[错误3]系统找不到指定的路径:   '\服务器\路径\文件夹/

我试过了:

\\servername\path\folder"\\servername\\path\\folder"

"\\servername\\path\\folder\\"r"\\servername\path\folder"

r"\\servername\path\folder\""\\\\servername\\path\\folder"

2 个答案:

答案 0 :(得分:2)

对我来说,以下两项工作

os.listdir(r'\\server\folder')
os.listdir('\\\\server\\folder')

答案 1 :(得分:0)

如果路径字符串不是由os.listdir(myUNCpath)这样的文字定义的,或者不是由myUNCpath = "\\\\servername\\dir1\\dir2"这样的原始字符串定义的,即使

myUNCpath = "\\servername\dir1\dir2不能正确处理Windows UNC路径,即使该字符串变量的定义也是如此因为listdir总是将字符串变量的反斜杠加倍。

但是,如果通过从ini文件或任何其他配置文件中读取UNC路径字符串来获得UNC路径字符串,该怎么办?

无法以文字形式进行编辑,也无法使用前面的r字符将其制成原始字符串。

作为一种变通方法,我发现可以将整个UNC路径字符串变量拆分为单个组件(以摆脱该模糊的反斜杠字符),并使用文字定义和通过此设置重新组合它再次反斜杠字符。然后字符串可以正常工作-递增,但正确!

这是我的功能,可以执行此工作。如果文件中的路径定义为,从函数返回的字符串将按预期工作 \ servername \ dir1 \ dir2(不添加反斜杠作为转义符)

...
myworkswellUNCPath = recomposeUNCpathstring(myUNCpath)

...

def recomposeUNCpathstring(UNCstring):

    pathstring1 = UNCstring.replace("\\\\", "").strip()
    pathComponents = pathstring1.split("\\")
    pathstring = "\\\\" + pathComponents[0] 

    for i in range(1, len(pathComponents)-1):
        pathstring = pathstring + "\\" + pathComponents[i]

    return pathstring

欢呼 斯蒂芬