banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

令人震驚的Python自動化腳本,你必須嘗試(5)

9、Readme.md 生成器它被認為是你倉庫中最重要的文件之一,但同時也是創建起來相當耗時的。

import os

def get_input(prompt, default=""):
    value = input(f"{prompt} [{default}]: ").strip()
    return value if value else default

def generate_readme():
    print("歡迎使用README.md!")
    print("請回答以下問題來生成您的README文件。如果要使用默認值,直接按回車即可。")

    # 獲取項目信息
    project_name = get_input("項目名稱")
    description = get_input("項目簡短描述")
    author = get_input("作者", os.getenv("USER", "Your Name"))
    
    # 獲取更多細節
    installation = get_input("安裝說明", "pip install your-package-name")
    usage = get_input("使用示例", "from your_package import main\n\nmain()")
    contributing = get_input("如何貢獻", "歡迎提交Pull Requests")
    license_type = get_input("許可證", "MIT")

    # 生成README內容
    readme_content = f"""# {project_name}

{description}

## 安裝

{installation}


## 使用

```python
{usage}

贡献#

{contributing}

许可证#

本项目采用 {license_type} 许可证。详情请见 LICENSE 文件。

作者#

{author}
"""

# 寫入README.md文件
with open("README.md", "w", encoding="utf-8") as f:
    f.write(readme_content)

print("README.md 已成功生成!")

if name == "main":
generate_readme()

10、OrganizeIT 2.0 這個自動化腳本可以幫助你在幾分鐘內整理你的文件夾。你只需要指定你想要清理的路徑,這個腳本將自動根據文件擴展名將所有文件分到不同的文件夾中。

import os
import hashlib
import shutil
from typing import Dict, Tuple

def get_file_hash(file_path: str) -> str:
    """計算文件的SHA-256哈希值"""
    with open(file_path, 'rb') as f:
        return hashlib.sha256(f.read()).hexdigest()

def create_folder(path: str) -> None:
    """創建文件夾,如果已存在則忽略"""
    os.makedirs(path, exist_ok=True)

def move_file(src: str, dst: str) -> None:
    """移動文件並打印操作信息"""
    shutil.move(src, dst)
    print(f"將 {os.path.basename(src)} 移動到 {os.path.dirname(dst)}")

def organize_and_move_duplicates(folder_path: str) -> None:
    """整理文件夾並移動重複文件"""
    extension_folders: Dict[str, str] = {}
    file_hashes: Dict[str, str] = {}
    duplicates_folder = os.path.join(folder_path, 'Duplicates')
    create_folder(duplicates_folder)

    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)
        if not os.path.isfile(file_path):
            continue

        _, extension = os.path.splitext(filename)
        extension = extension.lower()

        if extension not in extension_folders:
            destination_folder = os.path.join(folder_path, extension[1:] or 'NoExtension')
            create_folder(destination_folder)
            extension_folders[extension] = destination_folder

        file_hash = get_file_hash(file_path)

        if file_hash in file_hashes:
            move_file(file_path, os.path.join(duplicates_folder, filename))
        else:
            file_hashes[file_hash] = filename
            move_file(file_path, extension_folders[extension])

def main() -> None:
    folder_path = input("請輸入您要整理的文件夾路徑: ").strip()
    if not os.path.isdir(folder_path):
        print("輸入的路徑不是有效的文件夾。")
        return
    organize_and_move_duplicates(folder_path)

if __name__ == "__main__":
    main()
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。