【问题标题】:Linux is unable to find specified pathLinux 找不到指定路径
【发布时间】:2020-06-21 11:03:23
【问题描述】:

我的 python 脚本在 Windows 上运行没有任何问题。但是同样的脚本也必须在 Linux 机器上运行。运行时提示指定路径不存在。请注意,变量“path”指向云服务器

后来在浏览了一些论坛后尝试了os.path.join功能,也失败了

import os
import re
import sys

#List .xlsx files followed by the string ESC
path = '\\\\cloudnetworkonlinuxserver'
path2 = 'DBX'
path3 = 'SrcFiles'
path4 = 'MEBilling'
path5 = 'ParmFiles'

filenames = os.listdir(os.path.join(path, path2, path3, path4))
for filename in filenames:
    getdate = re.search('(?<=ESC_)\w+', filename)

    #Replace '_' with '-'
    if getdate:
        date = getdate.group(0).replace('_', '-')
        print('The following ESC file has date', date)

#Create .prm file with following body
f = open(os.path.join(path, path2, path5, "wf_SC_Monthend_Billing_XLS" + "." + "prm"), 'w')

#f.write cannot take more than one argument. Write variables such
a = '$$WF_PERIOD='
b = date

#Write in body of file
f.write("[Global]\n")
f.write('%s%s' % (a,b,))

#Close writing process
f.close

还有哪些其他方法可以指定与这两种操作系统兼容的路径?

【问题讨论】:

    标签: python import path operating-system


    【解决方案1】:

    假设 \\cloudnetworkonlinuxserver 是 Samba 共享:仅此路径是特定于 Windows 的。根据平台的不同,访问此共享的方式可能有所不同。

    在 Linux 上,您必须首先在某个物理路径上 mount this share,例如 /mnt/cloudshare。然后您将改为访问此路径。

    您应该从命令行参数或环境变量中获取此路径,以便可以根据每个环境中的正确情况传递适当的路径。

    或者,如果这应该是脚本的一部分,您必须让脚本负责挂载共享,以防环境是 Linux(理想情况下,在不再需要时也将其卸载)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 2018-12-12
      相关资源
      最近更新 更多