Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python编写的函数,用于列出特定文件夹中的文件,而不会过滤掉不需要的结果_Python_Python 3.x_Dictionary_If Statement - Fatal编程技术网

用Python编写的函数,用于列出特定文件夹中的文件,而不会过滤掉不需要的结果

用Python编写的函数,用于列出特定文件夹中的文件,而不会过滤掉不需要的结果,python,python-3.x,dictionary,if-statement,Python,Python 3.x,Dictionary,If Statement,此函数不会筛选出与前缀(~$)或扩展名(eval(不是“.xlsm”)匹配的文件,也不会筛选出文件夹 所有3次尝试都产生了相同的结果。我对Python这个东西还不太熟悉,所以请帮我把我应该做的事情说清楚 尝试1 def list_of_files(folder_path, prefix_to_filter_out, extension_to_filter_out): ... result = dict([(file, None) for file in os.listdi

此函数不会筛选出与前缀(~$)或扩展名(eval(不是“.xlsm”)匹配的文件,也不会筛选出文件夹

所有3次尝试都产生了相同的结果。我对Python这个东西还不太熟悉,所以请帮我把我应该做的事情说清楚

尝试1

def list_of_files(folder_path, prefix_to_filter_out, extension_to_filter_out):
    ...
        result = dict([(file, None) for file in os.listdir(folder_path)
                       if os.path.isdir(file) is False and
                       file.startswith(prefix_to_filter_out) is False and
                       file.endswith(extension_to_filter_out) is False])
    ...
    return result
尝试2

def list_of_files(folder_path, prefix_to_filter_out, extension_to_filter_out):
    ...
        result = dict([(file, None) for file in os.listdir(folder_path)
                       if not(os.path.isdir(file)) and
                       not(file.startswith(prefix_to_filter_out)) and
                       not(file.endswith(extension_to_filter_out))])
    ...
    return result
尝试3

def list_of_files(folder_path, prefix_to_filter_out, extension_to_filter_out):
    ...
        result = dict([(file, None) for file in os.listdir(folder_path)
                       if not(os.path.isdir(file))
                       if not(file.startswith(prefix_to_filter_out))
                       if not(file.endswith(extension_to_filter_out))])
    ...
    return result
我希望这能过滤掉Google Drive上以“~$”开头的临时文件,过滤掉文件夹,过滤掉Excel工作簿以外的任何内容。
但是,它包含了所有内容,甚至包括文件夹。 这是应用程序的一部分,每当添加/删除指定文件夹中的文件时,都会弹出消息框。这很快就会让人恼火,因为每当有人在Google Drive中打开或关闭文件时,它都会弹出

谢谢你的帮助

尝试4

def list_of_files(folder_path, prefix_to_filter_out, extension_to_focus_on):
    # Example:
    # folder_path = '/Google Drive/Box List/New Jobs',
    # prefix_to_filter_out = '~$',
    # extension_to_focus_on = '.xlsm'
    result = {}
    for file in os.listdir(folder_path):
        if os.path.isdir(os.path.join(folder_path, file)) is True:
            continue
        if file.startswith(prefix_to_filter_out):
            continue
        if not file.endswith(extension_to_focus_on):
            continue
        result[file] = None

    return result
    # Expected result:  {
    # 1741A Tegan Lee.xlsm,
    # 1741B Tegan Lee.xlsm,
    # 1741C Tegan Lee.xlsm,
    # 1742A WIZARD RENOVATIONS.xlsm,
    # 1742B WIZARD RENOVATIONS.xlsm}

    # Actual result:    {
    # 1741A Tegan Lee.xlsm,
    # 1741B Tegan Lee.xlsm,
    # 1741C Tegan Lee.xlsm,
    # 1742A WIZARD RENOVATIONS.xlsm,
    # 1742B WIZARD RENOVATIONS.xlsm
    # ~$1742A WIZARD RENOVATIONS.xlsm}
结果中不再有子文件夹,但前缀未正确筛选。 结果中不需要最后一个文件。。。它是Google Drive在文件打开时创建的临时文件。
但是,当我像这样手动输入前缀:file.startswith(“~$”)时,它就可以工作了。当我打印(前缀为filter out)时,它会吐出~$,因此这不是输入错误。

您只是缺少一些更精细的点

您的
文件
变量是
文件夹路径
中的文件名,而不是工作目录中的文件名
os.path.isdir
从外部查找
文件
,没有找到它,然后返回
False

os.path.isdir
通过提供整个路径
os.path.join(文件夹路径,文件)
来查找目录

continue
表示“转到下一个文件”

值得一提的是,多行
dict
函数调用是一种很难开发的方法。更简单的编码风格允许您一次一个地合并和测试这些过滤器

如果您还没有准备好处理单元测试,只需从外部运行函数,每次检查一个过滤器的结果:

  • 正在筛选文件夹?是的,让我们继续讨论前缀
  • 前缀得到过滤?是的,让我们继续讨论文件扩展名

第三次尝试的语法无效。虽然
为False,但您的第一个应该可以工作
是编写not的一种非常糟糕的方法。你的第二个解决方案确实有效。您没有提供正在使用的输入或预期的输出。“前缀
~?
”是什么意思
~?
Loook类似于正则表达式,但python
StartWith
endswith
仅使用子字符串匹配…经典问题:
os.path.isdir(file)
始终为false,因为您需要加入目录(file是文件名)。使用
os.path.isdir(os.path.join(文件夹路径,文件))
@Jean-Françoisfare Eagle eyed.
eval(不是.xlsm')
??除了为什么要创建字典之外,这可能是错误的?使用
设置
。您没有与文件关联的值。请提供一个或至少是过滤器参数的值/函数的完整调用谢谢Shay!这是朝着正确方向迈出的一步。我没有获取文件夹,我将扩展名改为“筛选”,将扩展名改为“聚焦”,这样在没有eval()的情况下更不容易出错。但是,我仍然在结果中得到那些该死的临时文件(以“~$”开头的文件)。您是否尝试在循环文件名时打印这些文件名,以查看Python到底看到了什么。听起来~$可能会被其他东西取代。是的。我现在已经打开了。真奇怪。当我手动输入时,结果与预期一致。我能想到的唯一一件事是,当它从文本文件导入它时,它会添加隐藏字符。非常奇怪,因为这样做:“~$filename”。startswith(“~$”)->trueok。所以我删除了所有隐藏的空字符('\x00')和返回字符('\n'),现在它工作得很好。谜团解开了。。。
def list_of_files(folder_path, prefix_to_filter_out, extension_to_filter_out):
    result = {}
    for file in os.listdir(folder_path):
        if os.path.isdir(os.path.join(folder_path, file)) is True:
            continue
        if file.startswith(prefix_to_filter_out):
            continue
        if file.endswith(extension_to_filter_out):
            continue
        result[file] = None

    return result    >>> list_of_files("some_directory", "~", ".xlsm") # should work