将目录更改为Python脚本的目录 [英] Change directory to the directory of a Python script
本文介绍了将目录更改为Python脚本的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用我的python脚本将目录更改为目录?到目前为止,我想我应该使用 os.chdir
和 sys.argv [0]
。我确定有一个更好的方法来编写自己的函数来解析argv [0]。
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].
推荐答案
os.chdir(os.path.dirname(__file__))
这篇关于将目录更改为Python脚本的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文