[Python] implementing piped xargs rm and tee command into python script

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python script that prints out file names according to specific wildcard patterns and characteristics. I then pipe a few commands to this printed out list, with xargs rm removing the listed files. This script is run on a unix server and ordinarily, I would run the script as follows:

    python deletefiles.py <directory> <wildcard1> | tee deletedfiles.txt | xargs rm

    this runs fine and all, but I am trying to find a way so that the tee and xargs rm command are in the script already so that the user doesn't have to type it into the command.

    i've looked online and people say to import subprocess, but i don't understand the full syntax of how to use it in junction with my script.

    as of right now, the code to run on a unix system is simply

    if len(sys.argv) == 3:
    try:
    input_directory = sys.argv[1]
    input_delete = sys.argv[2].split(',') \\patterns to delete
    except ValueError:
    "Please enter a valid input"


    what i am trying to do is implement a check for another argument, analyze/delete, where analyze just lets the script do it's job of printing out the file names and if the argument is delete, the script will xargs rm and tee those files.

    I'm not sure as to how to even approach this, as a large point of printing the files with my python script was so that xargs rm and tee deletedfiles.txt has something to go off of when deleting/saving to a text file. i was wondering if there was a way to do this conditionally within a python script

    if i need to provide any more information regarding my script, i am happy to oblige

    Continue reading...

Compartilhe esta Página