You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将/tmp目录下生成的.tar文件移动至桌面或指定位置?

解决方法:将生成的.tar文件从/tmp移动到指定位置

咱们核心要解决的问题是:先确定bash脚本生成的.tar文件的具体路径/名称,再用Python完成移动操作。下面给你几个实用的方案,按可靠程度排序:

方案1:让bash脚本输出生成的文件名(最推荐)

os.system没法捕获脚本的输出内容,所以咱们换成subprocess模块调用bash脚本,同时让createDevlicense.sh在执行完后输出它生成的.tar文件完整路径。

步骤1:修改bash脚本

在createDevlicense.sh的末尾添加一行,输出生成的tar文件路径(你需要替换成脚本里实际生成的文件名逻辑,比如根据传入的参数生成的名字):

# 假设你的脚本里是按flavor参数生成的文件名,替换成你实际的命名规则
GENERATED_TAR="/tmp/devlicense-${flavour}.tar"
echo "$GENERATED_TAR"

步骤2:修改Python代码

用subprocess.check_output捕获脚本输出的文件名,再用shutil.move(比os.rename更通用,跨目录/跨磁盘都能正常工作)移动到目标位置:

import subprocess
import shutil

# 目标位置,比如Linux桌面路径,Windows请换成类似"C:/Users/你的用户名/Desktop"
target_dir = "/home/你的用户名/Desktop"

for i in range(n):  # 注意:Python3里用range,xrange是Python2的语法
    print("---------------")
    print(" **** Creating Your License File **** ")
    try:
        # 调用bash脚本并捕获输出的tar文件路径
        tar_path = subprocess.check_output(
            ["./createDevlicense.sh", "-f", flavour, "-d", days, "-a", apid, "-t", type, "-w", warning, "-g", grace],
            text=True
        ).strip()  # 去掉输出里的换行和多余空格
        # 执行移动操作
        shutil.move(tar_path, target_dir)
        print(f"成功将 {tar_path} 移动到 {target_dir}")
    except subprocess.CalledProcessError as e:
        print(f"执行bash脚本出错:{e}")
    except FileNotFoundError:
        print(f"找不到生成的tar文件:{tar_path}")

方案2:如果文件名是固定的(简单直接)

如果bash脚本每次生成的tar文件名是固定的(比如/tmp/devlicense.tar),那直接在Python里写死路径,调用完脚本后执行移动:

import os
import shutil

fixed_tar_path = "/tmp/devlicense.tar"
target_dir = "/home/你的用户名/Desktop"

for i in range(n):
    print("---------------")
    print(" **** Creating Your License File **** ")
    os.system(f"./createDevlicense.sh -f {flavour} -d {days} -a {apid} -t {type} -w {warning} -g {grace}")
    # 先检查文件是否存在再移动
    if os.path.exists(fixed_tar_path):
        shutil.move(fixed_tar_path, target_dir)
        print(f"已将 {fixed_tar_path} 移动到 {target_dir}")
    else:
        print(f"错误:{fixed_tar_path} 文件不存在!")

方案3:通过文件名匹配(适合随机命名但有规律的情况)

如果生成的tar文件名是随机的,但有固定前缀(比如devlicense-*.tar),可以用glob模块匹配/tmp下的对应文件:

import os
import shutil
import glob

target_dir = "/home/你的用户名/Desktop"
# 匹配/tmp下前缀为devlicense的所有tar文件
tar_pattern = "/tmp/devlicense-*.tar"

for i in range(n):
    print("---------------")
    print(" **** Creating Your License File **** ")
    os.system(f"./createDevlicense.sh -f {flavour} -d {days} -a {apid} -t {type} -w {warning} -g {grace}")
    # 查找所有匹配的文件
    matching_tars = glob.glob(tar_pattern)
    if matching_tars:
        # 如果有多个匹配,取最新生成的(按文件修改时间排序)
        latest_tar = max(matching_tars, key=os.path.getmtime)
        shutil.move(latest_tar, target_dir)
        print(f"已将最新生成的 {latest_tar} 移动到 {target_dir}")
    else:
        print("在/tmp目录下找不到匹配的tar文件!")

注意事项

  • 确保Python脚本拥有读写/tmp目录和目标目录的权限;
  • 如果是Windows系统,路径格式要换成C:/tmp/xxx.tar或者C:\\tmp\\xxx.tar;
  • 优先用shutil.move而非os.rename,因为os.rename在跨文件系统移动时可能失败,shutil.move会自动处理复制+删除的逻辑。

内容的提问来源于stack exchange,提问作者user9809846

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:22:22