Python Forum

Full Version: four commands i wrote the other day
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i wrote these four commands the other day:

wait_for_all_files_to_exist.py:
#!/usr/bin/env python3
from sys import argv
from time import sleep,time as secs
import os

step = 1/4
exe = argv.pop(0)
nxet = int(secs())+0.5
if not argv:
    exit('need one or more file names')
while 1:
    e = 1
    for n in argv:
        if not os.path.exists(n):
            e = 0
    if e:
        break
    nxet += step
    diff = nxet-secs()
    while diff<0:
        nxet += step
        diff = nxet-secs()
    sleep(diff)
exit(0)
wait_for_all_files_to_not_exist.py:
#!/usr/bin/env python3
from sys import argv
from time import sleep,time as secs
import os

step = 1/4
exe = argv.pop(0)
nxet = int(secs())+0.5
if not argv:
    exit('need one or more file names')
while 1:
    e = 1
    for n in argv:
        if os.path.exists(n):
            e = 0
    if e:
        break
    nxet += step
    diff = nxet-secs()
    while diff<0:
        nxet += step
        diff = nxet-secs()
    sleep(diff)
exit(0)
wait_for_any_file_to_exist.py:
#!/usr/bin/env python3
from sys import argv
from time import sleep,time as secs
import os

step = 1/4
exe = argv.pop(0)
nxet = int(secs())+0.5
if not argv:
    exit('need one or more file names')
while 1:
    e = 0
    for n in argv:
        if os.path.exists(n):
            e = 1
    if e:
        break
    nxet += step
    diff = nxet-secs()
    while diff<0:
        nxet += step
        diff = nxet-secs()
    sleep(diff)
exit(0)
wait_for_any_file_to_not_exist.py:
#!/usr/bin/env python3
from sys import argv
from time import sleep,time as secs
import os

step = 1/4
exe = argv.pop(0)
nxet = int(secs())+0.5
if not argv:
    exit('need one or more file names')
while 1:
    e = 0
    for n in argv:
        if not os.path.exists(n):
            e = 1
    if e:
        break
    nxet += step
    diff = nxet-secs()
    while diff<0:
        nxet += step
        diff = nxet-secs()
    sleep(diff)
exit(0)
edit:

changed variable next to nxet.
Note that you are overwritting the built in function next

https://docs.python.org/3/library/functions.html#next Wrote:next(iterator[, default])
Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.
where have i done that? i don't see it (yet).

oh, now i see what you mean, the name. yeah bad choice of name; but it didn't break anything, the commands work.
It won't break anything in the code shown, it would be when you go to use next for its original intention and it doesn't do what you expected.
i changed it.