python-3.xfswatch

fswatch parsed argument not returning expected value?


I might misunderstand the xargs -I {} parsed argument of fswatch. This should return the file path of any new events in my specified directory, correct?

My command below is intended to watch /my/path/to/watch and trigger my_script.py when a new event occurs in /my/path/to/watch. my_script.py requires the file path associated with the new event, which is what I thought I was passing with {}.

My command:

fswatch -0  /my/path/to/watch | xargs -0 -n 1 -I {} python my_script.py {} > fswatch.output &

In my_script.py, I have

import sys
print sys.argv[0]

But this just returns my_script.py where I’m expecting it to return the file path associated with the new event in /my/path/to/watch.

What am I missing here?


Solution

  • When you are executing a program from your terminal, the first arguments will always be the name of your program, meaning sys.argv[0] will be the name of your script. What you want to use would probably be sys.argv[1], as everything after sys.argv[0] would be the actual parameters passed to the script.

    You could look at it like this:

    $ python my_script.py foo var
    

    sys.args would look like this:

    |     0        |  1  |  2  | 
    | my_script.py | foo | var |
    

    Basically, sys.args[0] would be the name of your script and sys.argv[1] and so on would be the actual parameters passed to it.