0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

フォルダ構成を一から作るの面倒じゃないですか?

Posted at

きっかけ

ツールの検証作業や勉強の時にプロジェクトのファイルを一から構成作成するのがめんどくさくないですか?
そんなあなたに朗報です。自動化スクリプト作成しました。

あまりちゃんと検証していません。ファイルツリーの中に含むことができない禁則文字列やタブに設定されたスペースの数などでエラーが発生することがあるかもしれません。サクッと(chatGPTが)作ったものなのでこの辺りはご容赦ください。

環境

  • window10
  • python 3.10 ... だったはず

フォルダツリーから自動でフォルダとファイルを生成するスクリプト

概要

このスクリプトは、フォルダ構造を記述したテキストファイルを読み込み、その構造に基づいてフォルダと空のファイルを自動で生成します。


スクリプト

import os

def create_structure_from_text(file_path):
    """
    フォルダツリーのテキストファイルを読み込んで、フォルダ・ファイルを作成する
    """
    with open(file_path, 'r') as file:
        lines = file.readlines()

    # カレントディレクトリを基準にフォルダ構造を作成
    current_path = os.getcwd()
    path_stack = [current_path]
    i = 0
    for line in lines:
        # インデントの深さを計算 (タブまたはスペースでインデント)
        depth = len(line) - len(line.lstrip())
        name = line.strip()
        # 初期depthを記録
        if i == 1:
            initial_depth = depth

        # path_stackの3つ目の要素に含まれる"\"を数える
        if i > 0:
            count = path_stack[-1].count("\\") - path_stack[0].count("\\")
            print("count", count)
            if depth / initial_depth < count:
                # 現在の深さに応じてスタックを調整
                while depth / initial_depth < (path_stack[-1].count("\\") - path_stack[0].count("\\")):
                    path_stack.pop()

        # 新しいフォルダまたはファイルのパスを構築
        new_path = os.path.join(path_stack[-1], name)

        # フォルダまたは空のファイルを作成
        if "." in name:  # ファイルの場合
            with open(new_path, 'w') as f:
                pass
        else:  # フォルダの場合
            os.makedirs(new_path, exist_ok=True)

        # スタックに追加
        if "." not in name:  # フォルダのみ追加
            path_stack.append(new_path)
            i += 1
    print("フォルダとファイルが正常に作成されました。")

# 使用例: フォルダツリーファイルを指定
text_file_path = "folder_structure.txt"  # フォルダ構造が記述されたテキストファイル
create_structure_from_text(text_file_path)

フォルダツリーの記述方法

スクリプトが読み込むテキストファイル(例: folder_structure.txt)には、以下のようにフォルダやファイルをインデント付きで記述します。

map_app
    app.py
    templates
        index.html
    static
        css
            styles.css
        js
            scripts.js
    requirements.txt
  • フォルダは拡張子がない名前で記述します。
  • ファイルは拡張子付きで記述します。
  • インデントはスペースまたはタブで表現します。

実行方法

  1. 上記のようなフォルダツリーファイル(例: folder_structure.txt)を作成します。
  2. スクリプトを保存して、create_structure.py という名前にします。
  3. 必要なフォルダツリーファイルを用意してスクリプトを実行します。
python create_structure.py

実行例

folder_structure.txt の内容:

project
    main.py
    src
        module.py
    tests
        test_module.py
    README.md

スクリプトを実行後、以下のようなフォルダとファイルが作成されます。

project/
├── main.py
├── src/
│   └── module.py
├── tests/
│   └── test_module.py
└── README.md

注意事項

  • ファイルの中身は空の状態で作成されます。
  • 既存のファイルやフォルダがある場合、上書きはされません。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?