2026年5月16日 星期六

只要點兩下,就能依據文字檔內容,創建對應的資料夾

         最近針對書本的目錄,需要創建相對應的資料夾。過去要找到該書的目錄網頁,一行一行地複製網頁文字,再一行一行地創建資料夾、重新命名、貼上。或是直接依據該書的紙本目錄,一行一行地看,創建資料夾、重新命名、一行一行地打字。
        Recently, I often need to create corresponding folders based on a book’s table of contents. In the past, I had to find the webpage containing the book’s contents, copy the text line by line, and then create folders one by one, rename them, and paste the titles manually. Another method was to look at the printed table of contents and type each line manually while creating and renaming folders one at a time.
         總覺得速度很慢,希望能夠將目錄網頁內容複製(或打入)到一個文字檔,程式依據該文字檔內容,一行一行地創建對應的資料夾。
         I always felt this process was too slow. I hope to be able to copy the contents from a webpage (or type them in) into a text file, and then have a program automatically create the corresponding folders line by line according to the contents of that text file.


下載檔案解壓密碼:demo1234
Here is the website where you can download the program and find instructions:
Download。Extraction Password: demo1234
使用教學(Instructional videos):








以下是開發過程與原始碼 (Development process and code):
程式名稱(Program name):pyTxt2Folder.py
程式內容(Code):
import os

# 開啟 input.txt,逐行讀取內容
for folder_name in open("InputFileName.txt", encoding="utf-8"):

    # 移除前後空白與換行符號
    folder_name = folder_name.strip()

    # 如果該行不是空白
    if folder_name:

        # 建立資料夾
        # exist_ok=True 表示資料夾已存在時不會報錯
        os.makedirs(folder_name, exist_ok=True)

        # 顯示建立成功訊息
        print(f"已建立資料夾:{folder_name}")

沒有留言:

張貼留言

只要點兩下,就能依據文字檔內容,創建對應的資料夾

         最近針對書本的目錄,需要創建相對應的資料夾。過去要找到該書的目錄網頁,一行一行地複製網頁文字,再一行一行地創建資料夾、重新命名、貼上。或是直接依據該書的紙本目錄,一行一行地看,創建資料夾、重新命名、一行一行地打字。         Recently, I oft...