0
0

作業ディレクトリ配下にある全てのプロジェクトフォルダをnode_modulesフォルダを除いてまるごとコピーしたい

Last updated at Posted at 2024-01-16

前の記事でディレクトリ構造を表示できるようになったので、それを従えてChatGPT4に表題の件を聞いてみた。

プロンプトは以下の通り。

Windowsの中にGitという作業用フォルダがあり、次のようなフォルダ構造になってます。
C:\Users\atyah\Git
├─ameblo_auto_iine
│  └─node_modules
│      ├─.bin
│      ├─agent-base
│      ├─async-limiter
│      ├─balanced-match
│      ├─brace-expansion
│      ├─buffer-from
│      ├─concat-map
│      ├─concat-stream
│      ├─core-util-is
│      ├─debug
│      ├─delay
│      ├─dotenv
│      ├─es6-promise
│      ├─es6-promisify
│      ├─extract-zip
│      ├─fd-slicer
│      ├─fs.realpath
│      ├─glob
│      ├─https-proxy-agent
│      ├─inflight
│      ├─inherits
│      ├─isarray
│      ├─mime
│      ├─minimatch
│      ├─minimist
│      ├─mkdirp
│      ├─ms
│      ├─once
│      ├─path-is-absolute
│      ├─pend
│      ├─process-nextick-args
│      ├─progress
│      ├─proxy-from-env
│      ├─puppeteer
│      ├─readable-stream
│      ├─rimraf
│      ├─safe-buffer
│      ├─string_decoder
│      ├─typedarray
│      ├─util-deprecate
│      ├─wrappy
│      ├─ws
│      └─yauzl
├─benzofaq(フォーラム)
├─benzoinfojapan
│  ├─.cache
│  ├─.firebase
│  ├─BIJsite_ver2.5
│  ├─node_modules
│  ├─public
│  ├─src
│  └─static
│      ├─icons
│      ├─pdf
│      ├─sound
│      └─twitterimg
├─BIJ_faq
│  ├─.firebase
│  ├─.vscode
│  ├─dist
│  ├─e2e
│  ├─functions
│  ├─node_modules
│  └─src
│      ├─app
│      ├─assets
│      └─environments
├─Codestep―学習サイト_WordPress
│  ├─HTMLCSS コーディング練習
│  └─バックアップ
├─docker-volumes
│  └─course38
├─portfolio-ポートフォリオサイト
│  ├─portfolio-web
│  ├─ダイアグラム図
│  ├─動画
│  └─画像
├─test-ナビゲーションバー(YouTubeより)
├─twitter-unfollowers
│  └─data
├─Udemy-docker
│  ├─copy
│  ├─docker-compose
│  ├─env
│  ├─mariadb
│  ├─run
│  └─test
├─UdemyーAIチャットアプリwithNext.jsとFirebase
│  └─ai-chatapp
│      ├─.firebase
│      ├─.next
│      ├─node_modules
│      ├─out
│      ├─public
│      └─src
├─UdemyーAWSフルスタック
│  └─todoApp
│      ├─client
│      └─server
├─UdemyーNext.js13とSpabaseでブログ
│  └─next.js13-tailwindcss-udemy
│      ├─.next
│      ├─public
│      └─src
├─UdemyーReact/Redux/TypeScript/Firebaseで作るアプリ
│  └─udemy-discord-clone
│      ├─.firebase
│      ├─build
│      ├─node_modules
│      ├─public
│      └─src
├─UdemyーRedux完全入門
│  ├─first-redux-app
│  └─redux-shoppingcart-app
│      ├─node_modules
│      ├─public
│      └─src
├─UdemyーTypeScript入門講座(タイパ重視)
│  └─TypeScript
│      ├─react-typescript
│      ├─Section1
│      ├─Section2
│      └─Section3
├─Udemyーちゃんと学ぶWordPress
│  ├─セクション3ーテーマ
│  ├─セクション4ーBlog
│  ├─セクション5ー移行
│  └─セクション6ーカスタマイズ
├─Udemy-HTML,CSS中上級コース
│  ├─Resources_GridLayout
│  ├─Resources_疑似要素
│  ├─Resources_セクション3(ヘッダーポジション、ハンバーガーメニュー、スライド)
│  ├─Resources_セクション4(メディアクエリ)
│  ├─Resources_セクション5(フクロウページ完成)
│  ├─Resources_セクション6~
│  └─Resources_セクション8(PCサイズレスポンシブ)
│      ├─Sample
│      └─Training
└─Udemy-PHP2ちゃんねる掲示板

このGitフォルダを、中にあるnode_modulesフォルダと.gitフォルダのみ除外してGit_backupフォルダにコピーしたい。
どのようにコーディングすれば目的を達成できるか?Pythonでコードを書いてください。

返ってきたコードがこちら。

import os
import shutil

def copy_directory(src, dst, ignore=None):
    if not os.path.exists(dst):
        os.makedirs(dst)
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.isdir(s):
            if item == 'node_modules' or item == '.git':
                continue
            copy_directory(s, d, ignore)
        else:
            if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
                print(f'Copying {s} to {d}')  # コピー中のファイルを表示
                shutil.copy2(s, d)

src_folder = 'Git'  # コピー元のディレクトリ
dst_folder = 'Git_backup'  # コピー先のディレクトリ

copy_directory(src_folder, dst_folder)

実行してみたが完璧。

ChatGPTの出現でプログラミングのハードルは確実に下がりました…:scream:

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