Python @ DjangoSpin

PyPro #52 List all text files in the current directory

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

List all text files in the current directory in Python

List all text files in the current directory in Python

List all text files in the current directory: Write a Python program to list all text files i.e. .txt files present in the current directory.

List all text files in the current directory

import glob

print( glob.glob('*.txt') )				
# returns a list of all .txt files in the current directory




# In order to check which is the current directory:
import os
print( os.getcwd() )

# To change current directory:
import os
os.chdir(path)					# e.g. os.chdir(r'C:\Users\')

For more on glob, click here.


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply