How to display filenames of word documents in a folder using python?
Question:
I want to display the filenames of word document present in a specified path using python.
Answers:
this could work for you:
for file in os.listdir(PATH):
if file.endswith(".doc") or file.endswith(".docx"):
print(file)
It could be just as simple as creating a custom iterator using scandir
passing the file extension (for word documents you can use docx). Like this:
import os
from typing import Type, Iterable
def scan_dir(path, file_ext) -> Iterable[Type[os.DirEntry]]:
for dir_entry in os.scandir(path):
if dir_entry.name.endswith(f'.{file_ext}'): yield dir_entry
if __name__ == '__main__':
for word_doc in scan_dir('.', 'docx'):
print(word_doc.name)
An alternative for @sxeros approach 🙂
import os
PATH = "your path"
files = list(filter(lambda s: ".doc" in s or ".docx" in s, os.listdir(PATH)))
print(files)
Two options:
1: glob
with regular expression
The glob
library is great for something like this (documentation):
import os
from glob import glob
word_docs = glob(os.path.join(PATH, "*.doc*"))
2: conditional list concatenation
This does everything using os
library alone:
import os
word_docs = [wd for wd in os.listdir(PATH) if wd.endswith(".docx") or wd.endswith(".doc")]
I want to display the filenames of word document present in a specified path using python.
this could work for you:
for file in os.listdir(PATH):
if file.endswith(".doc") or file.endswith(".docx"):
print(file)
It could be just as simple as creating a custom iterator using scandir
passing the file extension (for word documents you can use docx). Like this:
import os
from typing import Type, Iterable
def scan_dir(path, file_ext) -> Iterable[Type[os.DirEntry]]:
for dir_entry in os.scandir(path):
if dir_entry.name.endswith(f'.{file_ext}'): yield dir_entry
if __name__ == '__main__':
for word_doc in scan_dir('.', 'docx'):
print(word_doc.name)
An alternative for @sxeros approach 🙂
import os
PATH = "your path"
files = list(filter(lambda s: ".doc" in s or ".docx" in s, os.listdir(PATH)))
print(files)
Two options:
1: glob
with regular expression
The glob
library is great for something like this (documentation):
import os
from glob import glob
word_docs = glob(os.path.join(PATH, "*.doc*"))
2: conditional list concatenation
This does everything using os
library alone:
import os
word_docs = [wd for wd in os.listdir(PATH) if wd.endswith(".docx") or wd.endswith(".doc")]