在Windows目录中获取每个文件 [英] Getting Every File in a Windows Directory

查看:55
本文介绍了在Windows目录中获取每个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7中有一个文件夹,其中包含多个 .txt 文件。一个人如何获得上述目录中的每个文件的列表?

I have a folder in Windows 7 which contains multiple .txt files. How would one get every file in said directory as a list?

推荐答案

您可以使用 os.listdir(。) 列出当前目录(。)的内容:

You can use os.listdir(".") to list the contents of the current directory ("."):

for name in os.listdir("."):
    if name.endswith(".txt"):
        print(name)

如果要将整个列表作为Python列表,请使用列表理解

If you want the whole list as a Python list, use a list comprehension:

a = [name for name in os.listdir(".") if name.endswith(".txt")]

这篇关于在Windows目录中获取每个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆