Good to know a small delay works. The delay can probably be even smaller.
But checking for the existence of the gpio17 in a loop won't work 100% either.
Because the directory is first created with limited permissions.
After that the permissions are changed.
So actually, the wait should be for the correct permissions on the direction file.
Some info:
https://github.com/raspberrypi/linux/issues/553