Python获取脚本所在目录的正确方法
1.以前的方法
如果是要获得程序运行的当前目录所在位置,那么可以使用os模块的os.getcwd()函数。
如果是要获得当前执行的脚本的所在目录位置,那么需要使用sys模块的sys.path[0]变量或者sys.argv[0]来获得。实际上sys.path是Python会去寻找模块的搜索路径列表,sys.path[0]和sys.argv[0]是一回事因为Python会自动把sys.argv[0]加入sys.path。
具体来说,如果你在C:\test目录下执行python getpath\getpath.py,那么os.getcwd()会输出“C:\test”,sys.path[0]会输出“C:\test\getpath”。
更特别地,如果你用py2exe模块把Python脚本编译为可执行文件,那么sys.path[0]的输出还会变化:
如果把依赖库用默认的方式打包为zip文件,那么sys.path[0]会输出“C:\test\getpath\libarary.zip”;
如果在setup.py里面指定zipfile=None参数,依赖库就会被打包到exe文件里面,那么sys.path[0]会输出“C:\test\getpath\getpath.exe”。
2.正确的方法
但以上这些其实都不是脚本文件所在目录的位置。
比如C:\test目录下还有一个名为sub的目录;C:\test目录下有getpath.py,sub目录下有 sub_path.py,getpath.py调用sub_path.py;我们在C:\test下执行getpath.py。如果我们在 sub_path.py里面使用sys.path[0],那么其实得到的是getpath.py所在的目录路径“C:\test”,因为Python虚拟 机是从getpath.py开始执行的。如果想得到sub_path.py的路径,那么得这样:
os.path.split(os.path.realpath(__file__))[0]
其中__file__虽然是所在.py文件的完整路径,但是这个变量有时候返回相对路径,有时候返回绝对路径,因此 还要用os.path.realpath()函数来处理一下。也即在这个例子里,os.path.realpath(__file__)输出是 “C:\test\sub\sub_path.py”,而os.path.split(os.path.realpath(__file__))[0]输 出才是“C:\test\sub”。
3.实例说明
总之,举例来讲,os.getcwd()、sys.path[0] (sys.argv[0])和__file__的区别是这样的:
假设目录结构是:
代码如下:
C:test
[dir] getpath
[file] path.py
[dir] sub
[file] sub_path.py
然后我们在C:\test下面执行python getpath/path.py,这时sub_path.py里面与各种用法对应的值其实是:
os.getcwd() “C:\test”,取的是起始执行目录
sys.path[0]或sys.argv[0] “C:\test\getpath”,取的是被初始执行的脚本的所在目录
os.path.split(os.path.realpath(__file__))[0] “C:\test\getpath\sub”,取的是__file__所在文件sub_path.py的所在目录

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.
