Python 3.x python:使用os.system()运行多个命令并不会运行所有命令

Python 3.x python:使用os.system()运行多个命令并不会运行所有命令,python-3.x,command,os.system,Python 3.x,Command,Os.system,我有多个命令,希望使用os.system()和.py脚本运行它们。但是,我已经意识到有些命令没有执行,我无法找出问题所在。以下是my.py脚本的内容: import os def main(): commands= ['python -m venv test_venv & ' 'source ./test_venv/bin/activate &' 'pip install -U pip setuptools wheel & ' 'pip

我有多个命令,希望使用os.system()和.py脚本运行它们。但是,我已经意识到有些命令没有执行,我无法找出问题所在。以下是my.py脚本的内容:

import os

def main():
    commands= ['python -m venv test_venv & '
    'source ./test_venv/bin/activate &'
    'pip install -U pip setuptools wheel & '
    'pip install -U spacy & '
    'python -m pip freeze  > packages_information/spacy_packages.txt & '
    'python -m pip install -r packages_information/spacy_packages.txt & '
    'python -m spacy download de_core_news_sm & '
    'python -m pip freeze  > packages_information/de_core_news_sm_spacy_requirements.txt & '
    'python -m pip install -r  packages_information/de_core_news_sm_spacy_requirements.txt & '
    'pip uninstall de_core_news_sm & '
    'python -m spacy download de_dep_news_trf & '
    'python -m pip freeze  > packages_information/de_dep_news_trf_spacy_requirements.txt & '
    'python -m pip install -r  packages_information/de_dep_news_trf_spacy_requirements.txt &'
    'pip uninstall de_dep_news_trf & '
    'deactivate test_venv &'
    'rm -r test_venv']

        for command in commands:
    os.system(command)

    # clears out the files containing '.txtpython' extentions
    directory = os.curdir
    files_in_directory = os.listdir()

    filtered_files = [file for file in files_in_directory if file.endswith(".txtpython")]

    for file in filtered_files:
        path_to_file = os.path.join(directory, file)
        os.remove(path_to_file)

if __name__ == '__main__':
    main()
    

特别是Spacy的卸载和虚拟环境的删除/删除不会执行。

您的代码缩进错误。你能修好它吗?我发现在代码前后的行上使用三个反勾(`)来创建代码块比使用空格更容易。一个可能的问题是在每个命令的末尾都有一个与。IDK您正在使用的操作系统,但在类似unix的shell中,命令末尾的
&
会导致它在后台运行,因此您可能试图与其他命令并行运行delete/remove命令,这会导致它失败。