Python Forum
finding own python source file to read it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding own python source file to read it
#1
i would like to have some code be able to read its own python source file, perhaps by finding it somehow, opening that for reading, then reading it line by line. the difficulty i anticipate is because i want a module to be able to do (reading the modules own source) this and have the implementation be in a module. then what if it is being run from a .pyc or .pyo file (assuming the source exists). if the source is not present, then return something to indicate that or raise an exception.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I don't know whether it's useful information, but there is linecache - random access to text lines module which per documentation: "The linecache module allows one to get any line from a Python source file"
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Use the __file__ variable.
Reply
#4
it seems to work. i want to test this with a .pyc file but i can't seem to get python3 to make one anywhere. the man page is not helpful. does anyone know how to get it to do that?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 395 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Algorithm for extracting comments from Python source code Pavel1982 6 511 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Recommended way to read/create PDF file? Winfried 3 2,865 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,426 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,102 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,103 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,250 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,478 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,191 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,584 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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