Skip to content

Instantly share code, notes, and snippets.

@pshapiro
Created October 9, 2024 17:06
Show Gist options
  • Save pshapiro/67967bb964b80f85ec8b63d2c5384439 to your computer and use it in GitHub Desktop.
Save pshapiro/67967bb964b80f85ec8b63d2c5384439 to your computer and use it in GitHub Desktop.
The script I use to provide chatgpt with code files.
import os
import argparse
def list_specified_files(files, output_file):
# Open the output file in write mode
with open(output_file, 'w') as file_out:
for item in files:
if os.path.isfile(item):
# If it's a file, get the full path and write to the output file
full_path = os.path.abspath(item)
file_out.write(f"File Name: {os.path.basename(item)}\n")
file_out.write(f"Full Path: {full_path}\n")
# Try to read and dump file content
try:
with open(item, 'r') as f:
content = f.read()
file_out.write("File Content:\n")
file_out.write(content + "\n")
except Exception as e:
file_out.write(f"Error reading file: {str(e)}\n")
file_out.write("-" * 40 + "\n")
elif os.path.isdir(item):
# If it's a directory, traverse through and list the files
for root, dirs, files_in_dir in os.walk(item):
for file in files_in_dir:
full_path = os.path.join(root, file)
file_out.write(f"File Name: {file}\n")
file_out.write(f"Full Path: {full_path}\n")
# Try to read and dump file content
try:
with open(full_path, 'r') as f:
content = f.read()
file_out.write("File Content:\n")
file_out.write(content + "\n")
except Exception as e:
file_out.write(f"Error reading file: {str(e)}\n")
file_out.write("-" * 40 + "\n")
print(f"File list with contents saved to {output_file}")
def main():
# Initialize the argument parser
parser = argparse.ArgumentParser(description='List specified files and directories and save to a text file along with their contents.')
# Add arguments for the files and output file
parser.add_argument('files', nargs='+', type=str, help='The files or directories to list and dump contents.')
parser.add_argument('output_file', type=str, help='The output text file where the list and contents will be saved.')
# Parse the arguments
args = parser.parse_args()
# Call the function with the provided files and output file
list_specified_files(args.files, args.output_file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment