Python gets the correct method for the directory where the script "goes"

Source: Internet
Author: User
Tags python script

1. Previous methods

If you want to get the current directory where the program is running, you can use the OS.GETCWD () function of the OS module.

If you want to get the directory location of the currently executing script, you need to use the SYS module's sys.path[0] variable or sys.argv[0] to get it. The fact that Sys.path is Python will look for a list of search paths for modules, sys.path[0] and sys.argv[0] is one thing because Python automatically adds sys.argv[0] to Sys.path.

Specifically, if you execute Python getpath\getpath.py in the C:\test directory, then OS.GETCWD () outputs "C:\test" and sys.path[0] outputs "C:\test\getpath".

More specifically, if you compile the Python script as an executable using the Py2exe module, the output of sys.path[0] will change:
If the dependent library is packaged as a zip file by default, then Sys.path[0] will output "C:\test\getpath\libarary.zip";
If the Zipfile=none parameter is specified in the setup.py, the dependent library will be packaged into the exe file, then sys.path[0] will output "C:\test\getpath\getpath.exe".

2. The right approach

But these are not actually the location of the directory where the script files are located.
For example, there is a directory named sub under the C:\test directory, C:\test directory under the Getpath.py,sub directory has sub_path.py,getpath.py call sub_path.py; we are in C: \ Test to execute the getpath.py. If we use sys.path[0 in sub_path.py, we actually get the directory path "C:\test" where the getpath.py is located, because the Python virtual machine is executed from the beginning of the getpath.py. If you want to get the path to the sub_path.py, you get this:
Os.path.split (Os.path.realpath (__file__)) [0]

Although __file__ is the full path of the. py file, this variable sometimes returns a relative path, sometimes an absolute path, so it is also handled with the Os.path.realpath () function. In this case, the Os.path.realpath (__file__) output is "C:\test\sub\sub_path.py", while Os.path.split (Os.path.realpath (__file__)) [0] loses Out is the "C:\test\sub".

3. Example Description

In short, for example, the difference between OS.GETCWD (), sys.path[0] (sys.argv[0]) and __file__ is this:
Assume that the directory structure is:

Copy CodeThe code is as follows:
C:test

[Dir] GetPath

[File] path.py
[Dir] Sub

[File] sub_path.py


Then we execute the python getpath/path.py below the C:\test, at which point the values corresponding to each usage in sub_path.py are actually:
OS.GETCWD () "C:\test", which takes the starting execution directory
Sys.path[0] or sys.argv[0] "C:\test\getpath", which is the directory where the script was executed initially
Os.path.split (Os.path.realpath (__file__)) [0] "C:\test\getpath\sub", which is the directory where __file__ files sub_path.py

Python gets the correct method for the directory where the script "goes"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.