python - 需要一个脚本来遍历文件并执行命令

标签 python networking iteration rendering

请耐心等待,我以前没有使用过 python,我正在尝试尽快完成一些渲染,并因此停止在我的轨道上。

我正在将 .ifd 文件输出到网络驱动器 (Z:),它们存储在如下文件夹结构中;

Z:  
 - \0001 
 - \0002 
 - \0003

我需要遍历单个文件夹中的 ifd 文件,但文件数量不是静态的,因此还需要有一个可定义的范围(1-300、1-2500 等)。因此,该脚本必须能够为开始和结束范围采用额外的两个参数。

在每次迭代中,它使用此语句执行称为“咒语”的东西;

mantra -f file.FRAMENUMBER.ifd outputFile.FRAMENUMBER.png

我在 Internet 上找到了一个应该做类似事情的脚本;

import sys, os

#import command line args
args = sys.argv

# get args as string
szEndRange = args.pop()
szStartRange = args.pop()

#convert args to int
nStartRange = int(szStartRange, 10);
nEndRange = int(szEndRange, 10);
nOrd = len(szStartRange);

#generate ID range
arVals = range(nStartRange, nEndRange+1);


for nID in arVals:
   szFormat = 'mantra -V a -f testDebris.%%(id)0%(nOrd)dd.ifd' % {"nOrd": nOrd};
   line = szFormat % {"id": nID};
   os.system(line);

我遇到的问题是我无法让它工作。它似乎在迭代并做一些事情 - 但它看起来只是将 ifds 吐到某个地方的不同文件夹中。

TLDR;

我需要一个至少有两个参数的脚本;

  • 开始框架
  • endFrame

并从中创建一个 frameRange,然后使用它迭代所有执行以下命令的 ifd 文件;

  • mantra -f fileName.currentframe.ifd fileName.currentFrame.png

如果我能够指定文件名、文件目录和输出目录,那就太好了。我已经尝试手动执行此操作,但必须有一些我不知道的约定,因为当我尝试时它会出现错误(在冒号处停止)。

如果有人可以联系我或为我指明正确的方向,那就太棒了。我知道我应该尝试学习 Python,但我在渲染方面束手无策,需要帮助。

最佳答案

import os, subprocess, sys

if len(sys.argv) != 3:
    print('Must have 2 arguments!')
    print('Correct usage is "python answer.py input_dir output_dir" ')
    exit()

input_dir = sys.argv[1]
output_dir = sys.argv[2]
input_file_extension = '.txt'
cmd = 'currentframe'

# iterate over the contents of the directory
for f in os.listdir(input_dir):
    # index of last period in string
    fi = f.rfind('.')
    # separate filename from extension
    file_name = f[:fi]
    file_ext = f[fi:]
    # create args
    input_str = '%s.%s.ifd' % (os.path.join(input_dir, file_name), cmd)
    output_str =  '%s.%s.png' % (os.path.join(output_dir + file_name), cmd)
    cli_args = ['mantra', '-f', input_str, output_str]
    #call function
    if subprocess.call(cli_args, shell=True):
        print('An error has occurred with command "%s"' % ' '.join(cli_args))

这应该足以让您当前使用或稍作修改。

关于python - 需要一个脚本来遍历文件并执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29166746/

相关文章:

c++ - exp(-x) 和 exp(+x) 之间的泰勒级数差异

python - 如何快速打印Tensorflow结果到TXT?

Swift:AppDelegate 中 AppStart 处的网络请求 - ViewController 中的 CompletionHandler?

python - Openerp NotImplementedError : Iteration is not allowed on browse_record(stock. 移动,159275)

networking - haproxy 中每个后端的 maxconn 限制

linux - 需要Linux的端口映射解决方案

java - 打印出映射的键和值

python - Pycassa,线程池, "Exception in thread Thread-3 (most likely raised during interpreter shutdown):"

python - 将列表扩展为列表

Python使用pandas : how to Ignore delimiter within ""?