LoginSignup
1
0

More than 1 year has passed since last update.

ChatGPTでの開発に役立つソースコード

Posted at

import os
import fnmatch

# Define the pattern to match
pattern = "*.go"

# Find all files that match the pattern
for root, dirs, files in os.walk(".", topdown=False):
    for filename in files:
        if fnmatch.fnmatch(filename, pattern):
            # Print the file name with its path
            print("```"+os.path.join(root, filename))
            
            # Print the source code
            with open(os.path.join(root, filename), "r") as f:
                print(f.read())
            print("```\n")
python dirshown.py >> code
.txt

ディレクトリツリー追加版

import os
import fnmatch

def print_directory_tree(start_path):
    for root, dirs, files in os.walk(start_path, topdown=True):
        # Exclude .git directory
        if '.git' in dirs:
            dirs.remove('.git')
        
        level = root.replace(start_path, '').count(os.sep)
        indent = ' ' * 4 * level
        print('{}{}/'.format(indent, os.path.basename(root)))
        sub_indent = ' ' * 4 * (level + 1)
        for f in files:
            print('{}{}'.format(sub_indent, f))

# Print the directory tree
start_path = "."
print("```DirectoryTree")
print_directory_tree(start_path)
print("```\n")
# Define the pattern to match
pattern = "*.go"

# Find all files that match the pattern
for root, dirs, files in os.walk(".", topdown=False):
    for filename in files:
        if fnmatch.fnmatch(filename, pattern):
            # Print the file name with its path
            print("```"+os.path.join(root, filename))
            
            # Print the source code
            with open(os.path.join(root, filename), "r") as f:
                print(f.read())
            print("```\n")
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0