Recursively iterate through all subdirectories using pathlib
You can use the glob
method of a Path
object:
p = Path('docs')
for i in p.glob('**/*'):
print(i.name)
How to iterate through directories in Python using Pathlib
I think you want:
for a in source_path.glob("*/*/*"):
if a.is_dir():
print(a)
From the documentation: The **
pattern means “this directory and all subdirectories, recursively”, whereas one *
is a text wildcard.
recursively get all files except the hidden ones with pathlib
You could do something like this:
from pathlib import Path
def non_hidden_files(root):
for path in root.glob('*'):
if not path.name.startswith('.'):
if path.is_file():
yield str(path)
else:
yield from non_hidden_files(path)
print(*non_hidden_files(Path('.')))
Iterating through directories with Python
The actual walk through the directories works as you have coded it. If you replace the contents of the inner loop with a simple print
statement you can see that each file is found:
import os
rootdir = 'C:/Users/sid/Desktop/test'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print(os.path.join(subdir, file))
If you still get errors when running the above, please provide the error message.
Getting a list of all subdirectories in the current directory
Do you mean immediate subdirectories, or every directory right down the tree?
Either way, you could use os.walk
to do this:
os.walk(directory)
will yield a tuple for each subdirectory. Ths first entry in the 3-tuple is a directory name, so
[x[0] for x in os.walk(directory)]
should give you all of the subdirectories, recursively.
Note that the second entry in the tuple is the list of child directories of the entry in the first position, so you could use this instead, but it's not likely to save you much.
However, you could use it just to give you the immediate child directories:
next(os.walk('.'))[1]
Or see the other solutions already posted, using os.listdir
and os.path.isdir
, including those at "How to get all of the immediate subdirectories in Python".
How to use glob() to find files recursively?
pathlib.Path.rglob
Use pathlib.Path.rglob
from the pathlib
module, which was introduced in Python 3.5.
from pathlib import Path
for path in Path('src').rglob('*.c'):
print(path.name)
If you don't want to use pathlib, use can use glob.glob('**/*.c')
, but don't forget to pass in the recursive
keyword parameter and it will use inordinate amount of time on large directories.
For cases where matching files beginning with a dot (.
); like files in the current directory or hidden files on Unix based system, use the os.walk
solution below.
os.walk
For older Python versions, use os.walk
to recursively walk a directory and fnmatch.filter
to match against a simple expression:
import fnmatch
import os
matches = []
for root, dirnames, filenames in os.walk('src'):
for filename in fnmatch.filter(filenames, '*.c'):
matches.append(os.path.join(root, filename))
Iterate through a folder with the use of pathlib, I can´t open file with the use of path + value + string
Create the filename variable before passing it into pathlib.Path
.
i.e.
for kk in range(90):
var = TEST_NR[kk] + '.txt'
with open(folder / var ) as f:
Related Topics
Checking If Object on Ftp Server Is File or Directory Using Python and Ftplib
Getting Standard Errors on Fitted Parameters Using the Optimize.Leastsq Method in Python
Interact with Other Programs Using Python
Timedelta to String Type in Pandas Dataframe
Get Files Names Inside a Zip File on Ftp Server Without Downloading Whole Archive
Cx_Freeze Crashing Python 3.7.0
How to Normalize JSON Correctly by Python Pandas
When to Use Sys.Path.Append and When Modifying %Pythonpath% Is Enough
How to Read One Single Line of CSV Data in Python
Django Template System, Calling a Function Inside a Model
Differencebetween Join and Merge in Pandas
Argparse with Required Subparser
Find Out How Many Times a Regex Matches in a String in Python
Why Do Two Identical Lists Have a Different Memory Footprint