in Education by
Can someone tell me how to find all the files in directory with .txt extension in Python? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Their are many methods to do it: os.walk >>> import os >>> path = '/user/Bluetooth/cramp/charm' >>> text_files = [f for f in os.listdir(filedestination) if f.endswith('.txt')] >>> text_files ['qwe-cn.txt', 'zxc-jp.txt', 'qwe-kr.txt', 'qwe-tw.txt', ... 'mac-10.txt'] Or Glob >>> import glob >>> glob.glob('./*.txt') ['./Maths.txt', './physics.txt', './chem.txt', './bio.txt'] Or fnmatch.filter() import fnmatch, os print fnmatch.filter(os.listdir("/qwe"), "*.tx?") #

Related questions

0 votes
    I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I ... to this problem look like? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    In Python What command can I use to find these: Location of file which I am executing, and Current directory ( ... script in Python) Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to extract an extension from a filename in Python, How can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters? file. ... be a standard way. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    What is the best way to open a file as reading/write if it exists, or if it does not, then create it and ... to do the opening part. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Is there a way to get functionality similar to mkdir -p on the shell from within Python. I am looking for a ... has already written it? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    How to upgrade all python packages in one go using pip? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I am facing problems in _files which isn't defined if I execute the script with exec, ececfile and in _module_ ... help me with this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I check the file going to be written exists in the directory, and if not , How can I create directory ... " flag for the same? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I am a learner and just want to know the basic concept of super(). Here in the given code I want to know ... () ChildOne() ChildTwo() Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I import the functions from file.py to another Python file which is in: app/app1/Folder1/xyz. ... folder1.xyz import func_name Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to write a program which returns different values for different input,it can be easily done using switch ... some alternative? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to freeze the dos pop-up, I already tried raw-input as mentioned in my study material of Python 2.6 and ... (3**200) raw_input() Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
...