文件操作代码集合

发布于 2023-12-18  97 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

主要作者:ChatGPT-3.5

压缩文件夹内的子文件夹到单独的压缩包

import os
from zipfile import ZipFile

def compress_subfolders(folder_path):
    for root, dirs, files in os.walk(folder_path):
        for directory in dirs:
            folder_name = os.path.join(root, directory)
            zip_file_path = folder_name + '.zip'

            with ZipFile(zip_file_path, 'w') as zipf:
                for folder_root, folder_dirs, folder_files in os.walk(folder_name):
                    for file in folder_files:
                        file_path = os.path.join(folder_root, file)
                        arc_name = os.path.relpath(file_path, folder_name)
                        zipf.write(file_path, arc_name)

if __name__ == "__main__":
    current_folder_path = "."  # You can replace this with the path to your current folder
    compress_subfolders(current_folder_path)

创建大量新的空文件夹

#!/bin/bash

for i in {86..101}; do
    folder_name="weft_i"
    mkdir "folder_name"
    echo "Created folder: $folder_name"
done

修改文件夹中的文件名

import os
import re

def rename_files_with_underscore(folder_path):
    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)

        # Check if the file is a regular file and the filename matches the pattern
        if os.path.isfile(file_path) and re.match(r'^[a-zA-Z]+(\d+)\.zip$', filename):
            # Extract the alphanumeric part and insert an underscore before numeric characters
            new_filename = re.sub(r'(\d+)', r'_\1', filename)
            new_file_path = os.path.join(folder_path, new_filename)

            # Rename the file
            os.rename(file_path, new_file_path)
            print(f'Renamed: {filename} to {new_filename}')

if __name__ == "__main__":
    current_folder_path = "."  # You can replace this with the path to your current folder
    rename_files_with_underscore(current_folder_path)

AllCaseRun

#!/bin/bash

# 遍历当前目录中的所有项目
for item in *; do
    # 检查当前项目是否是文件夹
    if [ -d "item" ]; then
        echo "项目 'item' 是文件夹,执行 求解命令"

        # 进入文件夹
        cd "item"

        # 执行 foamFormatConvert 命令(假设 foamFormatConvert 是可执行文件)
        foamFormatConvert
        rm log.*
        ./Allrun

        # 返回上一级目录
        cd ..
    else
        echo "项目 'item' 不是文件夹"
    fi
done

将同类文件加入一个压缩包并删除原文件

import os
import zipfile
import polykriging as pk

# change current working directory
# os.chdir("./warpEven/")
files = pk.filenames("./", ".csv")

# sort the filenames
files = sorted(files, key=lambda x: int(x.split(".")[0].split("_")[1]))
# split filename by "_" and then remove the last element, which is the tow name

tow_names = set([file[:-len(file.split("_")[-1])-1] for file in files])

for tow_name in tow_names:

    with zipfile.ZipFile(tow_name + ".zip", 'w') as myzip:
        for file in files:
            if (tow_name + "_") in file:
                myzip.write(file, os.path.basename(file))
                os.remove(file)

统计剪贴板中的文本字数

import pyperclip

def count_words_in_clipboard():
    # 获取剪贴板中的文本
    clipboard_text = pyperclip.paste()

    # 统计字数
    word_count = len(clipboard_text.split()) # default is any whitespace

    return word_count

if __name__ == "__main__":
    # 调用函数并打印结果
    words_count = count_words_in_clipboard()
    print(f"剪贴板中的字数: {words_count}")
届ける言葉を今は育ててる
最后更新于 2023-12-30