Is there a way to list the files (not directories) in a directory with Python? I know I could use os.listdir
and a loop of os.path.isfile()
s, but if there's something simpler (like a function os.path.listfilesindir
or something), it would probably be better.
-
related– tshepangJan 5, 2013 at 23:21
8 Answers
This is a simple generator expression:
files = (file for file in os.listdir(path)
if os.path.isfile(os.path.join(path, file)))
for file in files: # You could shorten this to one line, but it runs on a bit.
...
Or you could make a generator function if it suited you better:
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
Then simply:
for file in files(path):
...
-
-
@ayvango That's just a different behaviour - there was no call for giving a full path in the question, and this works as-is in a well-defined way. Aug 8, 2013 at 21:34
-
os.listdir returns basenames of the files, and you may stat them directly by basename only if your current directory matches given path– ayvangoAug 8, 2013 at 22:07
-
1@ayvango I see what you mean - I was thinking about the returned values, not the check - you are indeed correct. I will update it. Aug 8, 2013 at 22:08
-
based on above answer, you could do:
files = list(filter(lambda x: os.path.isfile(x), os.listdir(path)))
Jul 7, 2017 at 3:06
files = next(os.walk('..'))[2]
-
2What's next? Single line answer cought my attention but I can't understand it.– KcFnMiMay 13, 2020 at 12:01
-
Using pathlib in Windows as follow:
files = (x for x in Path("your_path") if x.is_file())
Generates error:
TypeError: 'WindowsPath' object is not iterable
You should rather use Path.iterdir()
filePath = Path("your_path")
if filePath.is_dir():
files = list(x for x in filePath.iterdir() if x.is_file())
Since Python 3.6 you can use glob with a recursive option "**". Note that glob will give you all files and directories, so you can keep only the ones that are files
files = glob.glob(join(in_path, "**/*"), recursive=True)
files = [f for f in files if os.path.isfile(f)]
You could try pathlib, which has a lot of other useful stuff too.
Pathlib is an object-oriented library for interacting with filesystem paths. To get the files in the current directory, one can do:
from pathlib import *
files = (x for x in Path(".") if x.is_file())
for file in files:
print(str(file), "is a file!")
This is, in my opinion, more Pythonic than using os.path
.
See also: PEP 428.
-
4One common advice is to avoid that kind of import. That is, rather just import what you use, which in your case would just be
from pathlib import Path
.– tshepangJan 6, 2013 at 10:02 -
1While
pathlib
is great, I would argue it depends on the script at hand - if it only deals with files a couple of times, depending onpathlib
might be overkill. Jan 9, 2013 at 14:41
For the special case of working with files in the current directory, you could do it as a simple one-liner list comprehension:
[f for f in os.listdir(os.curdir) if os.path.isfile(f)]
Otherwise in the more general case, directory paths & filenames have to be joined:
dirpath = '~/path_to_dir_of_interest'
files = [f for f in os.listdir(dirpath) if os.path.isfile(os.path.join(dirpath, f))]
Using pathlib
, the shortest way to list only files is:
[x for x in Path("your_path").iterdir() if x.is_file()]
with depth support if need be.
If you use Python 3, you could use pathlib.
But, you have to know that if you use the is_dir()
method as :
from pathlib import *
#p is directory path
#files is list of files in the form of path type
files=[x for x in p.iterdir() if x.is_file()]
empty files will be skipped by .iterdir()
The solution I found is:
from pathlib import *
#p is directory path
#listing all directory's content, even empty files
contents=list(p.glob("*"))
#if element in contents isn't a folder, it's a file
#is_dir() even works for empty folders...!
files=[x for x in contents if not x.is_dir()]