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

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

最佳答案

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

关于python - 编写一个脚本来运行带参数的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50860166/

相关文章:

python - 如何使用不同的数据库设置(非默认设置)启动 Django 开发服务器

python - 带变化点的 PyMC3 回归

python - 为什么在Windows上datetime不能转换为<86400的时间戳?

Eclipse alt+shift+l 与窗口快捷方式冲突

python - PyQt,设置特定数量的行编辑输入

python - 查找句子中代词和名词之间的关系

windows - 阻止另一个程序获得焦点

windows - 使用 Windows 批处理脚本为文件夹中的每个文件应用命令

c - 我需要为 Dirent 结构分配内存吗

python - Beautifulsoup:解析html——获取href的一部分