【问题标题】:Python - Why wont wildcard bits work here?Python - 为什么通配符位在这里不起作用?
【发布时间】:2019-05-31 06:40:56
【问题描述】:

所以我试图将通配符位放在网络文件夹的路径中。路径在这里:

r"\\10.180.22.211\\Data\\DS~109803~Company~name of site\\Database"

DATA 和 Database 之间的部分发生了变化,但路径的其余部分保持不变。因此,当我实际将整个路径放入时,它工作得很好,但是如果我尝试将中间部分更改为通配符位,它就不起作用了。

r"\\10.180.22.211\\Data\\*\\Database"

OSError: [WinError 123] 文件名、目录名或卷标语法不正确:“\\10.180.22.211\\Data\\*\\Database”

我尝试过 、''、**、DS~*、109803 和其他几种组合,但没有一个带有通配符位的组合起作用。有人能解释一下为什么它们不能工作吗?是否有办法让通配符在这种情况下工作?

编辑:

这是我的更多代码:

import os, shutil
import glob

ip = "10.180.22.211"
#ip = input("Input your IP: ")


directorya = r"\\10.180.22.211\\Jobdata\\*\\Database"
#directorya = r"\\10.180.22.211\\Jobdata\\DS~109803000~customer~job site\\Database"
directoryb = "C:\\Users\\user\\Desktop\\test"

files = [file for file in os.listdir(directorya) if os.path.isfile(os.path.join(directorya, file))]
newest = max(glob.iglob(directorya + '\\*.*'), key=os.path.getctime)
print(newest)
shutil.copy(os.path.join(directorya, newest), directoryb)

【问题讨论】:

    标签: python wildcard


    【解决方案1】:

    你有两种方法:

    使用列表目录:

    for filename in os.listdir(path):
        ......
    

    或使用 glob:

    import glob
    path = "\10.180.22.211\Data\*\Database"
    for filename in glob.glob(path):
        ....
    

    获取最新文件:

        list_of_files = glob.glob('/path/to/folder/*')
        newest = max(list_of_files, key=os.path.getctime)
        print newest
    

    【讨论】:

    • 我在你的 latest = max(list_of_files…… 行现在出现错误。最新 = max(list_of_files, key=os.path.getctime) ValueError: max() arg is an empty sequence跨度>
    • 我将它添加到我的最后一条评论中。但它又来了。 latest = max(list_of_files, key=os.path.getctime) ValueError: max() arg is an empty sequence
    • 您似乎得到了空列表,可能与路径中的空格有关,尝试打印 list_of_files 以检查您得到了什么
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2013-05-16
    • 2019-02-10
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多