如何显示列表?

发布于 2024-12-11 23:09:10 字数 435 浏览 0 评论 0原文

我正在构建一个有效的简单函数(删除文件)。但最后我想返回已有效删除的文件列表。

这是函数:

def deleteFiles(files):
    # empty list
    RemovedFiles = list()

    for file in files:
        # Delete the file
        if os.remove(file):
            RemovedFiles.append(file)

    return RemovedFiles

我运行此函数:

print deleteFiles([/home/xyz/xyz.zip])

这有效地删除了 xyz.zip 但返回一个空列表:[]。我在这里做错了什么?

I'm building a simple function that works (delete the files). But In the end I want to return the list of the files that were effectively deleted.

This is the function:

def deleteFiles(files):
    # empty list
    RemovedFiles = list()

    for file in files:
        # Delete the file
        if os.remove(file):
            RemovedFiles.append(file)

    return RemovedFiles

I run this function with:

print deleteFiles([/home/xyz/xyz.zip])

This effectively deletes the xyz.zip but returns an empty list: []. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

固执像三岁 2024-12-18 23:09:10

问题是 os.remove 不返回任何内容。

不过,您可以尝试例外

for filename in filenames:
    try:
        # try to remove the file
        os.remove(filename)
    except OSError:
        pass
        # file not removed (maybe does not exist or is write-protected)
        # you may report an error here
    else:
        # this executes only if os.remove was successful
        RemovedFiles.append(filename)

The problem is that os.remove does not return anything.

You can however try and except:

for filename in filenames:
    try:
        # try to remove the file
        os.remove(filename)
    except OSError:
        pass
        # file not removed (maybe does not exist or is write-protected)
        # you may report an error here
    else:
        # this executes only if os.remove was successful
        RemovedFiles.append(filename)
平生欢 2024-12-18 23:09:10

os.remove() 返回 None,因此您永远无法追加到列表中。

os.remove() returns None, so you never get to append to the list.

枫以 2024-12-18 23:09:10

os.remove()< /a> 不返回值,因此您的 if 语句变得有效。

if None:
    RemovedFiles.append(file)

另一方面,如果无法删除文件,它会抛出异常,因此以下内容应该有效:

try:
    os.remove(file)
except OSError:
    pass
else:
    RemovedFiles.append(file)

os.remove() doesn't return a value, so your if statement becomes effectively

if None:
    RemovedFiles.append(file)

On the other hand, it does throw an exception if it can't remove the file, so the following should work:

try:
    os.remove(file)
except OSError:
    pass
else:
    RemovedFiles.append(file)
月竹挽风 2024-12-18 23:09:10

那是因为 os.remove() 不返回任何内容。您的 if 条件评估结果为 false。

试试这个:

def deleteFiles(files):
# empty list
RemovedFiles = list()

for file in files:
    # Delete the file
    try:
        os.remove(file)
        RemovedFiles.append(file)
    except OSError:
        continue

return RemovedFiles

这应该有效

That is because os.remove() does not return anything. Your if condition evaluates to false.

Try this:

def deleteFiles(files):
# empty list
RemovedFiles = list()

for file in files:
    # Delete the file
    try:
        os.remove(file)
        RemovedFiles.append(file)
    except OSError:
        continue

return RemovedFiles

This should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文