简体   繁体   English

如何列出目录的所有文件?

[英]How do I list all files of a directory?

import os print("Python Program to print list the files in a directory.") Direc = input(r"Enter the path of the folder: ") print(f"Files in the directory: {Direc}") files = listdir(Direc) files = [f for f in files if os.path.isfile(Direc+'/'+f)] #Filtering only the files. print(*files, sep="\n")

You can use isfile() to check whether an element is a file or a directory:您可以使用 isfile() 来检查元素是文件还是目录:

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

Or search for other alternatives as well here .或者也可以在此处搜索其他替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM