How can I iterate over files in a given directory?
--
Track title: CC E Schuberts Piano Sonata D 784 in A
--
Chapters
00:00 Question
00:22 Accepted answer (Score 1183)
01:37 Answer 2 (Score 208)
01:57 Answer 3 (Score 177)
02:40 Answer 4 (Score 74)
03:39 Thank you
--
Full question
https://stackoverflow.com/questions/1037...
Accepted answer links:
[os]: https://docs.python.org/3.6/library/os.h...
[pathlib]: https://docs.python.org/3.6/library/path...
[rglob]: https://docs.python.org/3/library/pathli...
[Path.glob()]: https://docs.python.org/3/library/pathli...
Answer 3 links:
[glob]: https://docs.python.org/3/library/glob.h...
Answer 4 links:
[os.scandir()]: https://docs.python.org/3/library/os.htm...
[source]: https://www.python.org/dev/peps/pep-0471/
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #iterator #directory
#avk47
ACCEPTED ANSWER
Score 1270
Python 3.6 version of the above answer, using os - assuming that you have the directory path as a str object in a variable called directory_in_str:
import os
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".asm") or filename.endswith(".py"):
# print(os.path.join(directory, filename))
continue
else:
continue
Or recursively, using pathlib:
from pathlib import Path
pathlist = Path(directory_in_str).glob('**/*.asm')
for path in pathlist:
# because path is object not string
path_in_str = str(path)
# print(path_in_str)
- Use
rglobto replaceglob('**/*.asm')withrglob('*.asm')- This is like calling
Path.glob()with'**/'added in front of the given relative pattern:
- This is like calling
from pathlib import Path
pathlist = Path(directory_in_str).rglob('*.asm')
for path in pathlist:
# because path is object not string
path_in_str = str(path)
# print(path_in_str)
Original answer:
import os
for filename in os.listdir("/path/to/dir/"):
if filename.endswith(".asm") or filename.endswith(".py"):
# print(os.path.join(directory, filename))
continue
else:
continue
ANSWER 2
Score 224
This will iterate over all descendant files, not just the immediate children of the directory:
import os
for subdir, dirs, files in os.walk(rootdir):
for file in files:
#print os.path.join(subdir, file)
filepath = subdir + os.sep + file
if filepath.endswith(".asm"):
print (filepath)
ANSWER 3
Score 187
You can try using glob module:
import glob
for filepath in glob.iglob('my_dir/*.asm'):
print(filepath)
and since Python 3.5 you can search subdirectories as well:
glob.glob('**/*.txt', recursive=True) # => ['2.txt', 'sub/3.txt']
From the docs:
The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched.
ANSWER 4
Score 28
Python 3.4 and later offer pathlib in the standard library. You could do:
from pathlib import Path
asm_pths = [pth for pth in Path.cwd().iterdir()
if pth.suffix == '.asm']
Or if you don't like list comprehensions:
asm_paths = []
for pth in Path.cwd().iterdir():
if pth.suffix == '.asm':
asm_pths.append(pth)
Path objects can easily be converted to strings.