I have a folder in Windows 7 which contains multiple .txt
files. How would one get every file in said directory as a list?
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.
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.
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")]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With