Python Forum
Get Current Directory From ShortCut.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Current Directory From ShortCut.
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Windows install does not create .exe, shortcut,etc to open hammer 4 2,094 May-05-2022, 12:33 PM
Last Post: hammer
  How to read python shortcut target profile directory of Chrome Ink file sunny9495 1 1,724 Apr-12-2022, 06:12 PM
Last Post: sunny9495
  portable way to get current directory Skaperen 5 3,295 Jun-12-2019, 01:16 PM
Last Post: Skaperen
  current directory issue saisankalpj 1 2,464 Nov-12-2018, 12:06 PM
Last Post: buran
  CWD (Current working directory) pcsailor 5 4,788 Nov-11-2018, 10:14 AM
Last Post: Larz60+
  how to create desktop shortcut shlomi27 3 4,125 Aug-23-2018, 09:26 PM
Last Post: buran
  How to run atom python script on idle press on keyboard shortcut. sonjoypall 2 5,204 Feb-07-2018, 07:51 AM
Last Post: buran
  Importing module from outside of the current directory Annie 4 4,697 Mar-21-2017, 11:13 PM
Last Post: Annie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020