[Python] Issues with using subprocess.run for a Jupyter Notebook python file

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm doing a data aggregation across multiple csv files. To save me some time, I have a python script developed that iterates through a series of subfolders and places a secondary script (called format_script_s&s).

    The script works in deploying the format_script_s&s.ipynb file into each subfolder. However, there is an error message when the script tries to run the format_script_s&s, from within each of the subfolders.

    I'm using the subprocess.run command to approach this. I think the error message is coming up because the file path is putting in double '//' into the file path. However I'm not sure and there's probably something important missing. I've done some research and the advice refers to the way that python interprets the 'backslash'.

    The other consideration is that I am working from a Jupyter notebook, and the file is a .ipynb file. Do I need to do something different with the subprocess.run command?

    I can't for the life of me work out how to correct the issue. Can anyone help?

    Code:

    import os
    import shutil
    import subprocess

    # Path to the folder containing subfolders
    # base_folder = "/path/to/your/folder"
    base_folder = "Police_data_complete"

    # Path to the script you want to place in each subfolder
    script_to_place = "format_scripts/format_script_s&s.ipynb"

    # Iterate through each item in the base folder
    for item in os.listdir(base_folder):
    item_path = os.path.join(base_folder, item)

    # Check if the item is a directory
    if os.path.isdir(item_path):
    try:
    # Copy the script into the subfolder
    destination_script = os.path.join(item_path, "format_script_s&s.ipynb")
    shutil.copy(script_to_place, destination_script)
    print(f"Copied script to: {destination_script}")

    # Run the script inside the subfolder
    result = subprocess.run(
    ["python", destination_script],
    cwd=item_path,
    capture_output=True,
    text=True
    )

    # Print the output of the script
    print(f"Output from {item}:\n{result.stdout}")
    if result.stderr:
    print(f"Errors from {item}:\n{result.stderr}")

    except Exception as e:
    print(f"An error occurred with {item}: {e}")


    Error message:

    Copied script to: data_complete\2022-04\format_script_s&s.ipynb
    data_complete\2022-04\format_script_s&s.ipynb
    Output from 2022-04:

    Errors from 2022-04:
    python: can't open file
    'C:\\path_to_main_folder\\data_complete\\2022-04\\data_complete\\2022-04\\format_script_s&s.ipynb': [Errno 2] No such file or directory

    Continue reading...

Compartilhe esta Página