GiuseppeP
GiuseppeP

Reputation: 81

Python app in autorun doesn't open file

I created an application that is running at windows startup, but every time it give me an Error:

[Errno 2] No such file or directory: 'user'

that error happen only at startup, if I open it normally (with doubleclick) it works good.

Note: I created the .exe with Pyinstaller and the file called 'user' is in the same directory of .exe (Program Files/App1/main.exe)

Maybe autorun works like a temp folder that can't recognize the content of Program Files directory?

Upvotes: 0

Views: 280

Answers (1)

abarnert
abarnert

Reputation: 366213

Your program should never count on the current working directory being the same as the directory it's run from. If the user runs your program from the command line, or you put it in a batch file, or you launch it from autorun, or another program tries to run it… in all those cases, the working directory will be somewhere else.

sys.argv[0] gives you the path to your program. So:

import sys
import os
scriptdir = os.path.dirname(os.path.abspath(sys.argv[0]))
userpath = os.path.join(scriptdir, 'user.exe')

Upvotes: 1

Related Questions