Converting a system command to python for file search and delete

前端 未结 2 1906
春和景丽
春和景丽 2021-01-15 15:45

I have a cron job that deletes files based on their age using the command:

find /path/to/file/ -type f -mmin +120|xargs -I file rm \'file\'

2条回答
  •  甜味超标
    2021-01-15 16:33

    use os.popen()

    >>>os.popen("find /path/to/file/ -type f -mmin +120|xargs -I file rm 'file'")
    

    or you can use the subprocess module:

    >>> from subprocess import Popen, PIPE
    >>> stdout= Popen(['ls','-l'], shell=False, stdout=PIPE).communicate()
    >>> print(stdout)
    

提交回复
热议问题