简体   繁体   English

如何在Windows中的Python2.7中获取当前目录的完整路径?

[英]How to get full path of current directory in Python2.7 in Windows?

I tried some of the answers to the existing post "How to get full path of current directory in Python?". 我尝试了现有文章“如何在Python中获取当前目录的完整路径?”的一些答案。 I'm using Python 2.7 on Windows. 我在Windows上使用Python 2.7。 If my full path is "c:\\alpha\\beta\\gamma", i get only "gamma" and not the full path. 如果我的完整路径是“ c:\\ alpha \\ beta \\ gamma”,那么我只会得到“ gamma”,而不是完整路径。 How should I coax python on windows not to swallow the prefix? 我应该如何在Windows上哄python不要吞下前缀?

This line of code should work for any file, as long as you are using Python 2.7. 只要您使用的是Python 2.7,这行代码就可以对任何文件起作用。 Using __file__ will use the current script for the file to locate. 使用__file__将使用当前脚本查找文件。

os.path.dirname(os.path.abspath(__file__))

The abspath() part is necessary to get the full absolute path name instead of simply the directory name. 要获取完整的绝对路径名,而不仅仅是目录名,必须使用abspath()部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM