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