[Python] How to access the name of a file A in file B where file B is imported into in file A

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I have a file that I have been importing into my homeworks that has several functions, one of which is

    def PDF(input_file:str, output_file:str):
    CODE TO MAKE A PDF OUT OF THE FILE I NEED TO TURN IN


    I would like to be able to just run PDF with no arguments and have it just choose the file it has been imported into and create an output file with the same name, but file extension .pdf.

    I'd like as little code as possible to be in file A so that each time I need to use file B, the extra code is minimal.

    I tried using name and file and code similar to

    def get_caller_filename():
    frame = inspect.currentframe().f_back
    file_name = frame.f_code.co_filename
    return file_name


    but they all return file B (the one I'm importing).

    Thank you in advance!!

    Continue reading...

Compartilhe esta Página