How do I get the path and name of the file that is currently executing?

后端 未结 29 2426
你的背包
你的背包 2020-11-22 06:43

I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process.

For example, let\'s say I have th

29条回答
  •  旧时难觅i
    2020-11-22 07:08

    Since Python 3 is fairly mainstream, I wanted to include a pathlib answer, as I believe that it is probably now a better tool for accessing file and path information.

    from pathlib import Path
    
    current_file: Path = Path(__file__).resolve()
    

    If you are seeking the directory of the current file, it is as easy as adding .parent to the Path() statement:

    current_path: Path = Path(__file__).parent.resolve()
    

提交回复
热议问题