Python创建软链接

Link.bat

@echo off
%1 mshta vbscript:createobject("shell.application").shellexecute("%~s0","::","","runas",1)(window.close)&exit
cd /d "%~dp0"
Link\Link.exe Link\Link.txt

在这里插入图片描述

link.txt

ProtocolBuffers\ProtocolCodes\<To>RPCClient\Assets\Scripts\ProtocolCodes\
ProtocolBuffers\ProtocolCodes\<To>RPCServer\Scripts\ProtocolCodes\

发布命令 pyinstaller -F --distpath 目标路径…\editor\ImageTools\Tools 脚本路径\Link.py

import os
import sys

# 检查命令行参数是否足够
if len(sys.argv) < 2:
    print("Usage: python script.py link.txt")
    sys.exit(1)

# 读取 link.txt 文件路径
link_file = sys.argv[1]

# 检查 link.txt 文件是否存在
if not os.path.exists(link_file):
    print(f"Link file '{link_file}' does not exist")
    sys.exit(1)

# 获取当前运行环境的路径
current_directory = os.path.dirname(os.path.realpath(sys.argv[0]))
current_directory = os.path.dirname(current_directory)

# 读取 link.txt 文件
with open(link_file, "r") as file:
    for line in file:
        # 按行分割数据
        src, dst = line.strip().split("<To>")
        src = os.path.join(current_directory, src.strip())  # 拼接源文件夹路径
        dst = os.path.join(current_directory, dst.strip())  # 拼接目标路径

        # 检查源文件夹是否存在
        if not os.path.exists(src):
            print(f"Source folder '{src}' does not exist")
            continue

        # 删除已存在的软链接(如果存在)
        if os.path.islink(dst):
            os.unlink(dst)

        # 创建软链接
        try:
            os.symlink(src, dst)
            print(f"Symbolic link created at '{dst}' pointing to '{src}'")
        except OSError as e:
            print(f"Error creating symbolic link: {e}")

# 等待用户关闭窗口
input("Press Enter to exit...")

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值