Reputation: 3
I want to iterate over a folder using a for-loop and os.listdir(), I'm just wondering what exactly to write into the brackets to access a particular file. Thanks!
Upvotes: 0
Views: 1894
Reputation: 16
The following example should help you:
import os
path = "C:/Users/TestDir"
dirs = os.listdir( path )
for file in dirs:
print(file)
Upvotes: 0
Reputation: 154
According to what i understood, you would like to iterate over files inside a particular folder until you find one particular file? You could do something like:
import os
FileToBeFound = "Your File Name To Find"
path = "Path to the directory you would like to check for a file in"
for file in os.listdir(path):
if file == FileToBeFound:
#do stuff with file
break
else:
continue
Upvotes: 0
Reputation: 127
You could easily google that. https://www.tutorialspoint.com/python/os_listdir.htm You only need the path in the brackets.
Upvotes: 0