Python: _ file _ and argv [0] ,__ file_argv

Source: Internet
Author: User

Python: _ file _ and argv [0] ,__ file_argv

In python, there are two methods to obtain the current master Script: sys. argv [0] and _ file __.

Sys. argv [0]

The best way to obtain the path of the master execution file is to use sys. argv [0], which may be a relative path. So it is safe to take abspath again, as shown in the following code:

import os,sysdirname, filename = os.path.split(os.path.abspath(sys.argv[0]))print "running from", dirnameprint "file is", filename
_ File __

_ File _ is used to obtain the path of the module, which may be a relative path, such as writing in the test. py script:

#! /Usr/bin/env python
Print _ file __

  • Run the command in the relative path./test. py,
  • Run the command in the absolute path to obtain the absolute path.
  • And execute it according to the user directory (~ /Practice/test. py), the obtained absolute path (~ Expanded)
  • To obtain the absolute path, we need OS. path. realpath (_ file __).

In the Python console, using print _ file _ directly leads to the "name' _ file _ 'is not defined" error, because it is not executed in any script, there is no definition of _ file.

_ File _ and argv [0]

There is no difference between the two in the main execution file, but it is different under different files. the following example:

C:\junk\so>type \junk\so\scriptpath\script1.pyimport sys, osprint "script: sys.argv[0] is", repr(sys.argv[0])print "script: __file__ is", repr(__file__)print "script: cwd is", repr(os.getcwd())import whereutilswhereutils.show_where() C:\junk\so>type \python26\lib\site-packages\whereutils.pyimport sys, osdef show_where():    print "show_where: sys.argv[0] is", repr(sys.argv[0])    print "show_where: __file__ is", repr(__file__)    print "show_where: cwd is", repr(os.getcwd()) C:\junk\so>\python26\python scriptpath\script1.pyscript: sys.argv[0] is 'scriptpath\\script1.py'script: __file__ is 'scriptpath\\script1.py'script: cwd is 'C:\\junk\\so'show_where: sys.argv[0] is 'scriptpath\\script1.py'show_where: __file__ is 'C:\\python26\\lib\\site-packages\\whereutils.pyc'show_where: cwd is 'C:\\junk\\so'

In general, argv [0] is more reliable.


Use of python for sysargv

1. sys. argv is used to store system parameters, including the script name itself.
2. It is difficult for you to directly use sys. argv [1] and sys. argv [2.

3. It is normal and recommended to use the corresponding library function to help you parse the corresponding function parameters.
For more information, see my:
[Arrangement] How to obtain and process command line parameters in Python
The sample code is provided.
4. After reading the above post, I want you to see it again:
[Arrangement] [Multi-graph explanation] How to Develop Python in Windows: How to Run Python scripts in cmd, how to use Python Shell (command line mode and GUI mode), and how to use Python IDE
Then you can understand. For you, you need to enter parameters when executing the script.
5. As wing ide does not input any parameters

List index out of range
.

After reading all the above posts, you will naturally understand it.

(No address is provided here. Please search for the title on google to find the address)

What can argv do in python? What can I say in detail? I don't know much about it in the book.

For more information, see my summary:
[Arrangement] How to obtain and process command line parameters in Python
After reading this, you can understand the functions and usage of sys. argv, and how to input and process parameters.

(No post address is provided here, so you can search for the title on google to find the post address)

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.