Here are a few ways to get all JSON file paths in a directory using Python:
- Using
glob (Simple and recommended):
import glob
import os
def get_json_files(directory):
# Get all .json files in the directory
json_files = glob.glob(os.path.join(directory, "*.json"))
return json_files
# Example usage
directory = "path/to/your/directory"
json_files = get_json_files(directory)
# Print all JSON file paths
for file_path in json_files:
print(file_path)
- Using
pathlib (Modern approach, Python 3.4+):
from pathlib import Path
def get_json_files(directory):
# Create Path object
path = Path(directory)
# Get all .json files
json_files = list(path.glob("*.json"))
# Convert to absolute paths (optional)
json_files = [file.absolute() for file in json_files]
return json_files
# Example usage
directory = "path/to/your/directory"
json_files = get_json_files(directory)
# Print all JSON file paths
for file_path in json_files:
print(file_path)
- Using
os.walk (If you need to search subdirectories recursively):
import os
def get_json_files(directory):
json_files = []
# Walk through directory and subdirectories
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".json"):
# Create full path
full_path = os.path.join(root, file)
json_files.append(full_path)
return json_files
# Example usage
directory = "path/to/your/directory"
json_files = get_json_files(directory)
# Print all JSON file paths
for file_path in json_files:
print(file_path)
- Using
pathlib with recursive search (Modern approach for subdirectories):
from pathlib import Path
def get_json_files(directory):
# Create Path object
path = Path(directory)
# Get all .json files recursively
json_files = list(path.rglob("*.json"))
# Convert to absolute paths (optional)
json_files = [file.absolute() for file in json_files]
return json_files
# Example usage
directory = "path/to/your/directory"
json_files = get_json_files(directory)
# Print all JSON file paths
for file_path in json_files:
print(file_path)
Key points to consider:
- Methods 1 and 2 only search the specified directory (not subdirectories)
- Methods 3 and 4 search the directory and all its subdirectories recursively
glob and pathlib approaches are generally preferred for their simplicity and cross-platform compatibility
pathlib is more modern and provides an object-oriented approach
- All paths returned are absolute paths if you use the
.absolute() method with pathlib
Example of how to use the results:
# Example of reading all JSON files
import json
json_files = get_json_files("path/to/your/directory")
for json_file in json_files:
try:
with open(json_file, 'r') as f:
data = json.load(f)
print(f"Contents of {json_file}:")
print(data)
except json.JSONDecodeError:
print(f"Error: {json_file} is not a valid JSON file")
except Exception as e:
print(f"Error reading {json_file}: {str(e)}")
Choose the method that best fits your needs:
- Use method 1 or 2 if you only need to search one directory
- Use method 3 or 4 if you need to search subdirectories
- Use
pathlib (methods 2 or 4) if you want a more modern, object-oriented approach
- Use
glob (method 1) if you want a simple, traditional approach