How to get only FILES in python?

Viewed 63

I just need to get the files in the directory. I use the os module for this. But I can't view only files in the directory.

os.chdir("/my/directory1") files=os.listdir(os.getcwd()) for f_name in files: print(f_name)

What should I do for this?

1 Answers

Try using os.path:

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