【问题标题】:How to apply a script to all files in extension and change names accordingly?如何将脚本应用于扩展名中的所有文件并相应地更改名称?
【发布时间】:2022-08-18 15:28:39
【问题描述】:

我有一个对文件进行某种转换的脚本。

pipeline.load_files(\'txt\', \'1.txt\')
pipeline.transform(\'function0\',\'file.jpg\', \'1.txt\')
pipeline.write(\'txt\', \'result.txt\', opt={\'c\':None})
pipeline.score(function=\'function1\', file =\'file.jpg\')
pipeline.score(function=\'function2\', file =\'file.jpg\')
pipeline.write_csv(\'result.csv\')

现在我想将此脚本用于我文件夹中的所有 100 个 txt 文件 (1.txt-100.txt)。所以最后我应该有 100 个名为 \'result1.csv\'...\'result100.csv\' 和 \'result1.txt\'....\'result100.txt\' 的文件。我怎样才能做到这一点?我知道它必须是一个 for 循环,但我不确定如何更改文件名。

  • 试试我的回答,如果您还有其他问题,请告诉我们

标签: python for-loop rename cycle batch-rename


【解决方案1】:

使用格式化字符串

for i in range(1, 101):
    # PUT YOUR CODE HERE
    pipeline.write_csv(f'result{i}.csv')

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多