how to loop through every file in directory python code example
Example 1: iterate through all files in directory python
import os
directory = 'the/directory/you/want/to/use'
for filename in os.listdir(directory):
if filename.endswith(".txt"):
continue
else:
continue
Example 2: how to loop through files in a directory python
import os
for filename in os.listdir(directory):
if filename.endswith(".asm") or filename.endswith(".py"):
continue
else:
continue
Example 3: python loop opening file from directory
basepath = "pathtodir/DataFiles/"
for filename in os.listdir(basepath):
if filename.endswith(".log"):
print(os.path.join("./DataFiles", filename))
with open(basepath + filename) as openfile:
for line in openfile:
........