How to list only text files in a directory using python?

0 votes

I am trying to list only text files in a directory (files with .txt extension). I got as far as listing only files and ignore directories but I am stuck.

from os import listdir
print(f for f in os.listdir('/home') if path.isfile(f))

How to list only the text files?

Feb 3, 2019 in Python by Sahil
496 views

1 answer to this question.

0 votes

Well, you are using a complex way. You can do it in a simple way. Try this:

import os
os.system("ls *.txt")
answered Feb 3, 2019 by Omkar
• 68,940 points

Related Questions In Python

0 votes
1 answer

How can I rename multiple files in a certain directory using Python?

Use os.rename(src, dst) to rename or move a file ...READ MORE

answered Nov 23, 2018 in Python by Aryya
• 7,350 points
606 views
0 votes
1 answer

How to Print a List in Python

ou are using Python 2.x syntax with ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,750 points
134 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 28, 2018 in Python by SDeb
• 13,230 points
107 views
0 votes
1 answer

List comprehension on a nested list - How to do it in Python?

Not sure what your desired output is, ...READ MORE

answered Nov 21, 2018 in Python by Nymeria
• 3,540 points
249 views
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 631 views
0 votes
0 answers
+4 votes
6 answers
0 votes
1 answer

How to list only files in a directory?

Before printing, you can use a check ...READ MORE

answered Feb 3, 2019 in Python by Omkar
• 68,940 points
46 views