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()
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.