编写一个运行带参数的命令的脚本

时间:2018-06-14 14:43:38

标签: python windows

如何在Windows中编写运行以下命令的脚本?

autoflake -i -r --remove-all-unused-imports %file_directory%

我的脚本如下所示:

file_directory = input("Enter directory name to run autoflake: ")

def autoflake_run():
    try:
        # I would like to run the command here.
    except:
        print("Path file error. Please make sure directory exists.")


autoflake_run()

1 个答案:

答案 0 :(得分:2)

使用subprocess module

import subprocess

file_directory = input('Enter directory name to run autoflake: ')


def autoflake_run():
    try:
        subprocess.run('autoflake -i -r --remove-all-unused-imports {}'
                       .format(file_directory))
    except:
        print('Path file error. Please make sure directory exists.')

autoflake_run()