R
R
Reshh4rd2018-05-25 18:17:40
Python
Reshh4rd, 2018-05-25 18:17:40

Why does not work out the deletion of files through the script?

Greetings!
Wrote a script that generates some files. A lot of files accumulate and need to be cleaned
Added to the script:
delete = subprocess.call (['/usr/bin/find', '/home/files/pars', '-type', 'd', '-mtime', ' +7', '-print0', '|', 'xargs', '-0', 'rm', '-rf'])
Running the script with this command produces the following:
/usr/bin/find: paths must precede expression
Usage: /usr/bin/find [-H] [-L] [-P] [path...] [expression]
I remove the entire structure for subprocess and run the same command from the command line:
/usr/bin/ find /home/files/pars -type d -mtime +7 -print0 | xargs -0 rm -rf
Everything is fine.
What's wrong with the script what part does he not understand? Failed to google..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav, 2018-05-25
@Nirail

Use "|" in subprocess is possible only if the shell=True parameter, but this is not safe, and can lead to various non-obvious problems.
If you need behavior like in the system, then you can use os.system (cmd).
The most correct solution would be to do a similar behavior, but based on Python.
If you do not want to reproduce the behavior of find, but you can run find in a subprocess, get its output (a list of files), which are then looped from Python and deleted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question