python编程

python_创建硬链接_Windows

2024-04-06  本文已影响0人  zip11

win系统,输入文件路径,创建硬链接,硬链接名字(不含扩展名)加上 _hard 字符

import os

def create_hard_link(source_file):

    try:
        # 获取源文件 的 文件夹路径
        source_folder = os.path.dirname(source_file)

        # 文件名 和 扩展名,分割 原始 文件名
        source_filename, source_ext = os.path.splitext(os.path.basename(source_file))
        
        # 构造目标硬链接文件的文件名
        link_filename = source_filename + "_hard" + source_ext
        
        # 构造目标硬链接文件的路径
        link_file = os.path.join(source_folder, link_filename)
        
        # 创建硬链接
        os.link(source_file, link_file)
        
        print(f"硬链接已成功创建: {link_file}")
    
    except Exception as e:
        print(f"创建硬链接时出错: {e}")

if __name__ == "__main__":

    print("创建文件 硬链接,Windows")

    # 输入文件路径
    source_file = input("请输入文件路径: ")
    
    # 检查输入的文件是否存在
    if not os.path.exists(source_file):
        print("输入的文件路径不存在!")
    else:
        # 创建硬链接
        create_hard_link(source_file)


上一篇 下一篇

猜你喜欢

热点阅读