How do I get the path of the current executed file in Python?

前端 未结 13 2362
天命终不由人
天命终不由人 2020-11-22 17:06

This may seem like a newbie question, but it is not. Some common approaches don\'t work in all cases:

sys.argv[0]

This means using path = os.path.abs

13条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 17:22

    Simply add the following:

    from sys import *
    path_to_current_file = sys.argv[0]
    print(path_to_current_file)
    

    Or:

    from sys import *
    print(sys.argv[0])
    

提交回复
热议问题