简体   繁体   English

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

[英]Write a script that runs a command with arguments

How can I write a script in Windows that runs the following command? 如何在Windows中编写运行以下命令的脚本?

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

My script looks like this: 我的脚本如下所示:

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()

Use the subprocess module . 使用subprocess模块

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM