Working with files
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answer ( 1 )
Method 1
import os
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(‘.txt’):
print file
Method 2
import os
path = ‘mypath/path’
files = os.listdir(path)
files_txt = [i for i in files if i.endswith(‘.txt’)]