Reputation: 3903
How do I get the absolute paths of all the files in a directory that could have many sub-folders in Python?
I know os.walk()
recursively gives me a list of directories and files, but that doesn't seem to get me what I want.
Upvotes: 115
Views: 217040
Reputation: 52
To get the absolute paths of all files in a directory (including all subdirectories) using Python, you can use os.walk()
combined with os.path.abspath()
to construct the absolute paths for each file.
import os
def get_all_file_paths(directory):
file_paths = []
for root, dirs, files in os.walk(directory):
for file in files:
# Construct absolute file path
absolute_path = os.path.abspath(os.path.join(root, file))
file_paths.append(absolute_path)
return file_paths
# Example usage:
directory_path = "/path/to/directory"
all_files = get_all_file_paths(directory_path)
# Print all absolute paths
for file_path in all_files:
print(file_path)
os.walk(directory)
recursively traverses the directory and its subdirectories.
root
: the current directory path.dirs
: a list of directories within root
.files
: a list of files in root
.For each file in the files
list, os.path.join(root, file)
is used to get the file's relative path.
os.path.abspath()
converts the relative path into an absolute path.
The absolute file paths are stored in the file_paths
list and returned.
The all_files
variable will contain a list of absolute file paths for all files in the given directory and its subdirectories.
Upvotes: 0
Reputation: 773
You can also select the absolute path of all files within a folder with glob.glob
function in case of all files have the same extension:
This command for example would extract all absolute paths of the whole .tif files within Downloads folder:
import glob
paths = glob.glob(r"C:\Downloads*.tif")
Upvotes: 0
Reputation: 152607
You can use stdlib pathlib
(or the backport if you have Python version < 3.4):
import pathlib
for filepath in pathlib.Path(directory).glob('**/*'):
print(filepath.absolute())
Upvotes: 36
Reputation: 9
Try This
pth=''
types=os.listdir(pth)
for type_ in types:
file_names=os.listdir(f'{pth}/{type_}')
file_names=list(map(lambda x:f'{pth}/{type_}/{x}',file_names))
train_folder+=file_names
Upvotes: -2
Reputation: 287775
os.path.abspath
makes sure a path is absolute. Use the following helper function:
import os
def absoluteFilePaths(directory):
for dirpath,_,filenames in os.walk(directory):
for f in filenames:
yield os.path.abspath(os.path.join(dirpath, f))
Upvotes: 112
Reputation: 422
All files and folders:
x = [os.path.abspath(os.path.join(directory, p)) for p in os.listdir(directory)]
Images (.jpg | .png):
x = [os.path.abspath(os.path.join(directory, p)) for p in os.listdir(directory) if p.endswith(('jpg', 'png'))]
Upvotes: 6
Reputation: 1477
Starting with python 3.5 the idiomatic solution would be:
import os
def absolute_file_paths(directory):
path = os.path.abspath(directory)
return [entry.path for entry in os.scandir(path) if entry.is_file()]
This not just reads nicer but also is faster in many cases. For more details (like ignoring symlinks) see original python docs: https://docs.python.org/3/library/os.html#os.scandir
Upvotes: 13
Reputation: 13349
Try:
from pathlib import Path
path = 'Desktop'
files = filter(lambda filepath: filepath.is_file(), Path(path).glob('*'))
for file in files:
print(file.absolute())
Upvotes: 1
Reputation: 362557
If the argument given to os.walk
is absolute, then the root dir names yielded during iteration will also be absolute. So, you only need to join them with the filenames:
import os
for root, dirs, files in os.walk(os.path.abspath("../path/to/dir/")):
for file in files:
print(os.path.join(root, file))
Upvotes: 43
Reputation: 463
for root, directories, filenames in os.walk(directory):
for directory in directories:
print os.path.join(root, directory)
for filename in filenames:
if filename.endswith(".JPG"):
print filename
print os.path.join(root,filename)
Upvotes: -1
Reputation: 193
from glob import glob
def absolute_file_paths(directory):
return glob(join(directory, "**"))
Upvotes: 3
Reputation: 298106
You can use os.path.abspath()
to turn relative paths into absolute paths:
file_paths = []
for folder, subs, files in os.walk(rootdir):
for filename in files:
file_paths.append(os.path.abspath(os.path.join(folder, filename)))
Upvotes: 9
Reputation: 35522
Try:
import os
for root, dirs, files in os.walk('.'):
for file in files:
p=os.path.join(root,file)
print p
print os.path.abspath(p)
print
Upvotes: 10