【通过一个脚本将多个实验按序跑起来,无需人工逐个调试 & Linux使用技巧】

1、通过一个脚本实现多个实验按序跑起来,无需人工逐个调试

任务描述

  • 在服务器上跑实验时,通常需要跑多次实验,但这些实验的差异之处仅有一两处参数的变动,如果在服务器盯着一个一个的跑,势必需要消耗大量的精力和时间;

  • 因此,将跑所有实验的指令全部汇总到一个脚本中,只需要run一个脚本即可,无需人工变更;

  • 还可以将每个跑完的实验结果专门存入txt文档,便于查阅细节

  • 脚本如下

    import os
    import subprocess
    
    # 定义要执行的命令列表
    commands = [
        'python crop.py -n 8 --crop-size 512 --image-folder',
        'python crop.py -n 8 --crop-size 256 --image-folder',
        'python crop.py -n 8 --crop-size 128 --image-folder'
        ]
    # 打开一个文件用于保存结果
    with open('result.txt', 'w') as f:
        # 循环执行命令
        for cmd in commands:
        	
        	count=commands.index(cmd)
        	count+=1
        	print("现在开始执行第 ",count,"条命令了,共计",len(commands),"条指令!")
            # 执行命令并获取结果
            result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            # 将结果写入文件
            f.write(f'Command: {cmd}\n')
            f.write(f'Stdout: {result.stdout.decode()}\n')
            f.write(f'Stderr: {result.stderr.decode()}\n')
            f.write('------------------------\n')
    

2、Linux使用技巧

问题描述

  • 在Linux系统如何查看一个文件夹下共有多少个文件

  • 在Linux系统如何查看一个文件夹下共有多少种类型的文件,分别是那几种类型

    ls -l | grep "^-" | wc -l ## 查看文件夹下共有多少个文件
    
    find . -type f -exec file {
          
          } \; | awk -F: '{print $2}' | sort | uniq -c ## 查看文件夹下共有多少种类型的文件,分别是那几种类型
    

猜你喜欢

转载自blog.csdn.net/crist_meng/article/details/131689118