Python Forum

Full Version: Get Current Directory From ShortCut.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, My question is regarding file Directories in Windows based Systems:
Suppose I have a Directory : D:\PyProj\
In that Directory I have:
---- A Text file : D:\PyProj\Assets\Data.txt
---- A Python file : D:\PyProj\meow.py

And I have A shortcut : C:\Woof.lnk Which is linked to the above python file.

If meow.py is launched using Woof.lnk How may I read Data.txt?
Provided, the Path of the shortcut Woof.lnk May Change.
Example tested works.
Data.txt:
Output:
42
# meow.py
with open(r'G:\div_code\PyProj\Assets\Data.txt') as f:
    print(f.read())

input('Press any key to quit')
Output:
42
So giving absolute path to Data.txt and other option is the package way with importlib.resources
Also running code from shortcut the window will disapred fast,so add input() to stop it so can se result.