Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Every File in a Windows Directory

I have a folder in Windows 7 which contains multiple .txt files. How would one get every file in said directory as a list?

like image 322
rectangletangle Avatar asked Apr 12 '11 00:04

rectangletangle


People also ask

How do I show all files in a directory in command prompt?

Navigate to the directory containing the folders you wish to appear in your list. Click in the address bar and replace the file path by typing cmd then press Enter. This should open a black and white command prompt displaying the above file path. Type dir /A:D.

How do I see everything in a folder?

Select the Start button, then select Control Panel > Appearance and Personalization. Select Folder Options, then select the View tab. Under Advanced settings, select Show hidden files, folders, and drives, and then select OK.


1 Answers

You can use os.listdir(".") to list the contents of the current directory ("."):

for name in os.listdir("."):     if name.endswith(".txt"):         print(name) 

If you want the whole list as a Python list, use a list comprehension:

a = [name for name in os.listdir(".") if name.endswith(".txt")] 
like image 88
Greg Hewgill Avatar answered Oct 19 '22 17:10

Greg Hewgill