Change directory to the directory of a Python script [duplicate]
问题 This question already has answers here : How do I change the working directory (cd) in Python? (12 answers) Closed 11 months ago . How do i change directory to the directory with my python script in? So far I figured out I should use os.chdir and sys.argv[0] . I'm sure there is a better way then to write my own function to parse argv[0]. 回答1: os.chdir(os.path.dirname(__file__)) 回答2: os.chdir(os.path.dirname(os.path.abspath(__file__))) should do it. os.chdir(os.path.dirname(__file__)) would