Python get path comparison of different methods

Source: Internet
Author: User

In the software often need to get the path of the file, there are many ways (such as Os.path.realpath (__file__), OS.GETCWD (), Os.path.abspath (__file__), sys.path[0], Sys.argv[0]), but each has a different, compared to the following:

In a nipt_analysis-v1.3.02.3.py program, write the test code as follows,

Temp_file = open ("X:/wzd/temp/tempfile.txt", "W")
Curr_path0 = Os.path.realpath (__file__)
Curr_path1 = OS.GETCWD ()
Curr_path2 = Os.path.abspath (__file__)
Curr_path3 = sys.path[0]
Curr_path4 = sys.argv[0]
Path0 = Os.path.dirname (Curr_path0)
path1 = Os.path.dirname (curr_path1)
path2 = Os.path.dirname (curr_path2)
Path3 = Os.path.dirname (CURR_PATH3)
Path4 = Os.path.dirname (CURR_PATH4)
Temp_file.write ("Curr_path0" + "\ T" +curr_path0+ "\ T" + "Path0" + "\ T" +path0+ "\ n")
Temp_file.write ("curr_path1" + "\ T" +curr_path1+ "\ T" + "path1" + "\ T" +path1+ "\ n")
Temp_file.write ("curr_path2" + "\ T" +curr_path2+ "\ T" + "path2" + "\ T" +path2+ "\ n")
Temp_file.write ("curr_path3" + "\ T" +curr_path3+ "\ T" + "path3" + "\ T" +path3+ "\ n")
Temp_file.write ("Curr_path4" + "\ T" +curr_path4+ "\ T" + "PATH4" + "\ T" +path4+ "\ n")
Temp_file.close ()

Place the program under directory X:\WZD\NIPT_Analysis_software\NIPT_Analysis-V1.3.02.3,

Then write a. bat file to invoke the nipt_analysis-v1.3.02.3.py program. Place the. bat file under directory X:\WZD\NIPT_Analysis_software,

After you run the. bat file, you are finished with the "X:/wzd/temp/tempfile.txt file" and the result is:

Then use Pyinstaller to package the nipt_analysis-v1.3.02.3.py into a nipt_analysis-v1.3.02.3.exe file and replace the. py file,

Then use the. bat file to invoke the Nipt_analysis-v1.3.02.3.exe program. Place the. bat file under directory X:\WZD\NIPT_Analysis_software,

The results of the operation are as follows:

Finally select Curr_path4 = Sys.argv[0] As the way to get the nipt_analysis-v1.3.02.3.py file path.

Python get path comparison of different methods

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.