开发者

Get the directory of a Shortcut calling a Python Script

How does one acquire the directory of the Windows Shortcut calling a py开发者_StackOverflowthon script. I'd like to have multiple shortcuts in various places pointing to one script that I could edit if I wanted, but where the directory from which the script is called is accessible. Is this possible?

This script would likely then be compiled with py2exe or something, so if it is something that isn't possibly until THAT stage, I could go with that. Thanks!


The easiest solution is to just use a batch script instead of a shortcut.

All it needs is python C:\path\to\script\script.py, and the script will have the correct CWD.

The default when creating a shortcut on windows is for the Start in: property to be set to the folder the linked file resides in.

The script has no knowledge of the fact that it was called from a shortcut, let alone where that shortcut resides.

You can change the Start in: property of the shortcut to the path of the folder the shortcut resides in.

Then you can use os.getcwd() to get that path.

Unfortunately setting Start in: to . doesn't work.


In general you cannot obtain this information. You should use argv to switch behaviour of your script.


you can do so with

import os
print os.getcwd()

os.getcwd()

Return a string representing the current working directory. Availability: Unix, Windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜