Browse files and subfolders in Python
You can use os.walk()
to recursively iterate through a directory and all its subdirectories:
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith((".html", ".htm")):
# whatever
To build a list of these names, you can use a list comprehension:
htmlfiles = [os.path.join(root, name)
for root, dirs, files in os.walk(path)
for name in files
if name.endswith((".html", ".htm"))]
Search multiple files in folder and sub folders python
Search and print:
import os
def search( folder, word ):
for root, dirs, files in os.walk(folder):
path = root.split(os.sep)
for filename in files:
fullname = os.path.join(root, filename)
if open( fullname ).read().find( word ) != -1:
print( fullname )
search( "FolderName", 'any text')
Search and backup searched lines:
import os
def search( folder, word, bfile ):
for root, dirs, files in os.walk(folder):
path = root.split(os.sep)
for filename in files:
fullname = os.path.join(root, filename)
if open( fullname ).read().find( word ) != -1:
print( fullname )
blines = [x for x in open(fullname).readlines() if x.find(word) != -1]
open( bfile, 'a' ).writelines(blines)
search( "FolderName", 'any text', 'backup-lines.file')
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".
Python list directory, subdirectory, and files
Use os.path.join
to concatenate the directory and file name:
for path, subdirs, files in os.walk(root):
for name in files:
print(os.path.join(path, name))
Note the usage of path
and not root
in the concatenation, since using root
would be incorrect.
In Python 3.4, the pathlib module was added for easier path manipulations. So the equivalent to os.path.join
would be:
pathlib.PurePath(path, name)
The advantage of pathlib
is that you can use a variety of useful methods on paths. If you use the concrete Path
variant you can also do actual OS calls through them, like changing into a directory, deleting the path, opening the file it points to and much more.
How can I search sub-folders using glob.glob module?
In Python 3.5 and newer use the new recursive **/
functionality:
configfiles = glob.glob('C:/Users/sam/Desktop/file1/**/*.txt', recursive=True)
When recursive
is set, **
followed by a path separator matches 0 or more subdirectories.
In earlier Python versions, glob.glob()
cannot list files in subdirectories recursively.
In that case I'd use os.walk()
combined with fnmatch.filter()
instead:
import os
import fnmatch
path = 'C:/Users/sam/Desktop/file1'
configfiles = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in fnmatch.filter(files, '*.txt')]
This'll walk your directories recursively and return all absolute pathnames to matching .txt
files. In this specific case the fnmatch.filter()
may be overkill, you could also use a .endswith()
test:
import os
path = 'C:/Users/sam/Desktop/file1'
configfiles = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in files if f.endswith('.txt')]
Python search files in multiple subdirectories for specific string and return file path(s) if present
great to have you here!
What you have done so far is found all the file paths, now the simplest way is to go through each of the files, read them into the memory one by one and see if the name you are looking for is there.
import glob
files = glob.glob('C:\TEMP' + '/**', recursive=True)
target_string = 'John Smit'
# itereate over files
for file in files:
try:
# open file for reading
with open(file, 'r') as f:
# read the contents
contents = f.read()
# check if contents have your target string
if target_string in conents:
print(file)
except:
pass
This will print the file path each time it found a name.
Please also note I have removed the second line from your code, because it is redundant, you initiate the list in line 3 anyway.
Hope it helps!
Find all files in a directory with extension .txt in Python
You can use glob
:
import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)
or simply os.listdir
:
import os
for file in os.listdir("/mydir"):
if file.endswith(".txt"):
print(os.path.join("/mydir", file))
or if you want to traverse directory, use os.walk
:
import os
for root, dirs, files in os.walk("/mydir"):
for file in files:
if file.endswith(".txt"):
print(os.path.join(root, file))
Search a folder and sub folders for files starting with criteria
This can be done using the os
and shutil
modules:
import os
import shutil
Firstly, we need to establish the source and destination paths. source
should the be the directory you are copying and destination
should be the directory you want to copy into.
source = r"/root/path/to/source"
destination = r"/root/path/to/destination"
Next, we have to check if the destination
path exists because shutil.copytree()
will raise a FileExistsError
if the destination
path already exists. If it does already exist, we can remove the tree and duplicate it again. You can think of this block of code as simply refreshing the duplicate directory:
if os.path.exists(destination):
shutil.rmtree(destination)
shutil.copytree(source, destination)
Then, we can use os.walk
to recursively navigate the entire directory, including subdirectories:
for path, _, files in os.walk(destination):
for file in files:
if not file.startswith("4") and len(os.path.splitext(file)[0]) != 7:
os.remove(os.path.join(path, file))
if not os.listdir(path):
os.rmdir(path)
We then can loop through the files in each directory and check if the file does not meet your condition (starts with "4" and has a length of 7). If it does not meet the condition, we simply remove it from the directory using os.remove
.
The final if-statement
checks if the directory is now empty. If the directory is empty after removing the files, we simply delete that directory using os.rmdir
.
find files in subdirectories only with python
your loop gives you the path to the current folder, you can check if it is different than the rootDir
(provided rootDir
is a full path ofcourse):
for folder, subfolders, files in os.walk(rootDir):
if folder != rootDir:
for f in files:
print(f)
Read subfolder python
I think you messed your order up a little bit. If you do subdir = os.listdir(sd_path)
before the loop you can't possibly get the sub directories because you need to use the parent directory to get to them.
So in your loop after you checked that an "entry" is a folder you can store the absolute path of this folder in a variable and then list it's contents with os.listdir().
Then you can loop through those and parse them.
How I would do it:
import os
r_path='//esw-fs01/esw_niagara_no_bck/BuildResults/master/0.1.52.68_390534/installation_area/autotestlogs_top/'
root = os.listdir(r_path)
for entry in root:
# print(entry)
subdir_path = os.path.join(r_path, entry) # create the absolute path of the subdir
if os.path.isdir(subdir_path): # check if it is a folder
subdir_entries = os.listdir(subdir_path) # get the content of the subdir
for subentry in subdir_entries:
subentry_path = os.path.join(subdir_path, subentry) # absolute path of the subentry
# here you can check everything you want for example if the subentry has a specific name etc
print(subentry_path)
Related Topics
Importerror: No Module Named Win32Api
Importerror: No Module Named Tensorflow
Using Monotonically_Increasing_Id() for Assigning Row Number to Pyspark Dataframe
How to Resize an Image with Opencv2.0 and Python2.6
Removing Control Characters from a String in Python
Value Error Trying to Install Python for Windows Extensions
How to Find Out My Pythonpath Using Python
Putting a 'Cookie' in a 'Cookiejar'
Permanently Adding a File Path to Sys.Path in Python
Zip with List Output Instead of Tuple
Pyplot Move Alternative Y Axis to Background
Filtering a List Based on a List of Booleans
Adding a Particle Effect to My Clicker Game
Namespaces with Module Imports