Combine Files Script
2025-01-234 turns5,019 charsgpt-4o
Summary
User requested a script to combine all files in a directory into a single output file with filenames recorded, excluding the venv directory.
Messages
write a one file program which will take everything in its directory and output it as a single file with the names of each file recorded before the content as well. I want to be able to run this program in the root of a program and to output a txt file that simple has the read contents from all of the files in the entire directory recursively
Here’s a Python program that accomplishes your requirements. The program reads all the files in its directory and subdirectories recursively, then writes their names and contents to a single text file. Save this code as a Python file (e.g., `combine_files.py`) and run it in the root directory of your program.
```python
import os
def combine_files_in_directory(output_file="combined_output.txt"):
"""
Combines all files in the current directory (recursively) into a single output file.
The file names are recorded before their contents.
"""
with open(output_file, "w", encoding="utf-8") as outfile:
for root, _, files in os.walk(os.getcwd()):
for file in files:
file_path = os.path.join(root, file)
try:
with open(file_path, "r", encoding="utf-8") as infile:
# Write the file name and a separator
outfile.write(f"--- {file_path} ---\n")
# Write the file content
outfile.write(infile.read())
outfile.write("\n\n")
except Exception as e:
# Log an error if a file couldn't be read
outfile.write(f"--- {file_path} (ERROR: {e}) ---\n\n")
if __name__ == "__main__":
combine_files_in_directory()
```
### Features:
1. **Recursive File Search**: The script uses `os.walk` to traverse the directory tree recursively.
2. **File Names as Headers**: Each file's content is preceded by its absolute path.
3. **Error Handling**: If a file can't be read (e.g., binary files or permission issues), it records the error in the output.
4. **Encoding Support**: Opens all files in UTF-8 encoding to handle text files.
### Output:
The output file (`combined_output.txt`) will look like this:
```
--- /path/to/directory/file1.txt ---
<contents of file1.txt>
--- /path/to/directory/subdir/file2.txt ---
<contents of file2.txt>
--- /path/to/directory/subdir/file3.log ---
<contents of file3.log>
--- /path/to/directory/image.png (ERROR: 'utf-8' codec can't decode byte ...) ---
```
Place the script in the root directory of your project, then run it using:
```bash
python combine_files.py
```
This will create a `combined_output.txt` file in the same directory. Let me know if you'd like enhancements, such as filtering files by extension!
gpt-4o
except now have it ignore the venv directory
To ignore the `venv` directory (or any other directory), you can modify the code to skip it during the recursive traversal. Here's the updated program:
```python
import os
def combine_files_in_directory(output_file="combined_output.txt", ignore_dirs=None):
"""
Combines all files in the current directory (recursively) into a single output file.
The file names are recorded before their contents.
Directories in `ignore_dirs` will be skipped.
"""
if ignore_dirs is None:
ignore_dirs = ["venv"] # Default to ignoring 'venv'
with open(output_file, "w", encoding="utf-8") as outfile:
for root, dirs, files in os.walk(os.getcwd()):
# Modify the dirs list in-place to skip ignored directories
dirs[:] = [d for d in dirs if d not in ignore_dirs]
for file in files:
file_path = os.path.join(root, file)
try:
with open(file_path, "r", encoding="utf-8") as infile:
# Write the file name and a separator
outfile.write(f"--- {file_path} ---\n")
# Write the file content
outfile.write(infile.read())
outfile.write("\n\n")
except Exception as e:
# Log an error if a file couldn't be read
outfile.write(f"--- {file_path} (ERROR: {e}) ---\n\n")
if __name__ == "__main__":
combine_files_in_directory()
```
### Changes Made:
1. **Ignored Directories**:
- Added an `ignore_dirs` parameter, defaulting to `["venv"]`.
- Modified the `dirs` list in-place using `dirs[:] = [...]` to skip the ignored directories during traversal.
2. **Explanation**:
- The `dirs[:]` modification ensures that `os.walk` doesn't descend into the ignored directories, making it efficient.
### How to Use:
Run the script the same way:
```bash
python combine_files.py
```
If you want to ignore additional directories (e.g., `node_modules`, `.git`), modify the `ignore_dirs` list like this:
```python
combine_files_in_directory(ignore_dirs=["venv", "node_modules", ".git"])
```
Let me know if you want to add more customizations!
gpt-4o