簡體   English   中英

在 Windows 中運行帶有一組文件的程序的 Python 腳本

[英]Python script to run a program with a set of files in Windows

我有一個 Windows 應用程序,它將在目錄中的一系列輸入文件上運行。

操作順序是:

  1. 轉到所需目錄
  2. 在第一個輸入文件上運行程序
  3. 轉到生成結果的相對路徑
  4. 將文件(程序生成同名)重命名並復制到所需目錄中
  5. 轉到第 2 步並使用下一個文件繼續其余步驟

我知道我可以使用導入子進程並使用 subprocess.call() 來運行 Windows shell 命令。 我如何以有效的方式實現所有這些步驟?

任何提示都會有所幫助。

感謝和問候,桑托什

根據您的描述,我想這樣的事情應該可行。 只需更改路徑和名稱。

import os
import shutil

files = [r"C:\Path\To\File1", r"C:\Path\To\File2"]
new_names = ["new_file1", "new_file2"]


def x(file, new_name):
    program_path = r"C:\Path\To\Program"
    save_path = r"C:\Path\To\Save"
    new_path = r"C:\NewPath\To\Folder"
    os.system("%s -argument %s" % (program_path, file))
    shutil.move(save_path, new_path + '/' + new_name)


y = 0
for file_ in files:
    x(file_, new_names[y])
    y += 1

嘗試提供您到目前為止所做的事情,以便您得到更好的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM