Python获取当前路径
如下的代码列出了Python中获取当前路径的一些方法:
import os import sys def test(): path1 = '/dir1/dir2/file.txt' path2 = '/dir1/dir2/file.txt/' print('__file__ =', __file__) print('path1 =', path1) print('path2 =', path2) # Return the directory name of pathname path. # This is the first element of the pair returned # by passing path to the function split(). print('os.path.dirname(__file__) =', os.path.dirname(__file__)) print('os.path.dirname(path1) =', os.path.dirname(path1)) print('os.path.dirname(path2) =', os.path.dirname(path2)) # Return the base name of pathname path. This is the second element # of the pair returned by passing path to the function split(). # Note that the result of this function is different from the # Unix basename program; where basename for '/foo/bar/' returns # 'bar', the basename() function returns an empty string (''). print('os.path.basename(__file__) =', os.path.basename(__file__)) print('os.path.basename(path1) =', os.path.basename(path1)) print('os.path.basename(path2) =', os.path.basename(path2)) # Return a string representing the current working directory. print('os.getcwd() =', os.getcwd()) # Return a normalized absolutized version of the pathname path. # On most platforms, this is equivalent to calling the function # normpath() as follows: normpath(join(os.getcwd(), path)). print('os.path.abspath(__file__) =', os.path.abspath(__file__)) print('os.path.abspath(path1) =', os.path.abspath(path1)) # Return the canonical path of the specified filename, eliminating # any symbolic links encountered in the path (if they are supported # by the operating system). print('os.path.realpath(__file__) =', os.path.realpath(__file__)) print('os.path.realpath(path1) =', os.path.realpath(path1)) # Split the pathname path into a pair, (head, tail) where tail is the # last pathname component and head is everything leading up to that. # The tail part will never contain a slash; if path ends in a slash, # tail will be empty. If there is no slash in path, head will be empty. # If path is empty, both head and tail are empty. Trailing slashes are # stripped from head unless it is the root (one or more slashes only). # In all cases, join(head, tail) returns a path to the same location # as path (but the strings may differ). Also see the functions dirname() # and basename(). print('os.path.split(__file__) =', os.path.split(__file__)) print('os.path.split(path1) =', os.path.split(path1)) # A list of strings that specifies the search path for modules. # Initialized from the environment variable PYTHONPATH, plus an # installation-dependent default. # # As initialized upon program startup, the first item of this list, # path[0], is the directory containing the script that was used to # invoke the Python interpreter. If the script directory is not # available (e.g. if the interpreter is invoked interactively or if # the script is read from standard input), path[0] is the empty string, # which directs Python to search modules in the current directory first. # Notice that the script directory is inserted before the entries # inserted as a result of PYTHONPATH. # # A program is free to modify this list for its own purposes. # Only strings and bytes should be added to sys.path; all # other data types are ignored during import. print('sys.path[0] =', sys.path[0]) sys.path.insert(0, path1) print('sys.path[0] =', sys.path[0]) # The list of command line arguments passed to a Python script. # argv[0] is the script name (it is operating system dependent # whether this is a full pathname or not). If the command was # executed using the -c command line option to the interpreter, # argv[0] is set to the string '-c'. If no script name was passed # to the Python interpreter, argv[0] is the empty string. print('sys.argv[0] =', sys.argv[0]) test()
代码脚本所在的绝对路径为:
/home/hyg/code/pytorch-study/vehicle-reid/some_test.py
执行如下命令:
cd /home/hyg/code/pytorch-study/vehicle-reid
python /home/hyg/code/pytorch-study/vehicle-reid/some_test.py
输出为:
__file__ = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py path1 = /dir1/dir2/file.txt path2 = /dir1/dir2/file.txt/ os.path.dirname(__file__) = /home/hyg/code/pytorch-study/vehicle-reid os.path.dirname(path1) = /dir1/dir2 os.path.dirname(path2) = /dir1/dir2/file.txt os.path.basename(__file__) = some_test.py os.path.basename(path1) = file.txt os.path.basename(path2) = os.getcwd() = /home/hyg/code/pytorch-study/vehicle-reid os.path.abspath(__file__) = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py os.path.abspath(path1) = /dir1/dir2/file.txt os.path.realpath(__file__) = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py os.path.realpath(path1) = /dir1/dir2/file.txt os.path.split(__file__) = ('/home/hyg/code/pytorch-study/vehicle-reid', 'some_test.py') os.path.split(path1) = ('/dir1/dir2', 'file.txt') sys.path[0] = /home/hyg/code/pytorch-study/vehicle-reid sys.path[0] = /dir1/dir2/file.txt sys.argv[0] = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py
执行如下命令:
cd /home/hyg/code/pytorch-study/vehicle-reid
python ./some_test.py
输出为:
__file__ = ./some_test.py path1 = /dir1/dir2/file.txt path2 = /dir1/dir2/file.txt/ os.path.dirname(__file__) = . os.path.dirname(path1) = /dir1/dir2 os.path.dirname(path2) = /dir1/dir2/file.txt os.path.basename(__file__) = some_test.py os.path.basename(path1) = file.txt os.path.basename(path2) = os.getcwd() = /home/hyg/code/pytorch-study/vehicle-reid os.path.abspath(__file__) = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py os.path.abspath(path1) = /dir1/dir2/file.txt os.path.realpath(__file__) = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py os.path.realpath(path1) = /dir1/dir2/file.txt os.path.split(__file__) = ('.', 'some_test.py') os.path.split(path1) = ('/dir1/dir2', 'file.txt') sys.path[0] = /home/hyg/code/pytorch-study/vehicle-reid sys.path[0] = /dir1/dir2/file.txt sys.argv[0] = ./some_test.py
执行如下命令:
cd /home/hyg/code/pytorch-study/vehicle-reid
python some_test.py
输出为:
__file__ = some_test.py path1 = /dir1/dir2/file.txt path2 = /dir1/dir2/file.txt/ os.path.dirname(__file__) = os.path.dirname(path1) = /dir1/dir2 os.path.dirname(path2) = /dir1/dir2/file.txt os.path.basename(__file__) = some_test.py os.path.basename(path1) = file.txt os.path.basename(path2) = os.getcwd() = /home/hyg/code/pytorch-study/vehicle-reid os.path.abspath(__file__) = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py os.path.abspath(path1) = /dir1/dir2/file.txt os.path.realpath(__file__) = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py os.path.realpath(path1) = /dir1/dir2/file.txt os.path.split(__file__) = ('', 'some_test.py') os.path.split(path1) = ('/dir1/dir2', 'file.txt') sys.path[0] = /home/hyg/code/pytorch-study/vehicle-reid sys.path[0] = /dir1/dir2/file.txt sys.argv[0] = some_test.py
执行如下命令:
cd /home/hyg/code
python ./pytorch-study/vehicle-reid/some_test.py
输出为:
__file__ = ./pytorch-study/vehicle-reid/some_test.py path1 = /dir1/dir2/file.txt path2 = /dir1/dir2/file.txt/ os.path.dirname(__file__) = ./pytorch-study/vehicle-reid os.path.dirname(path1) = /dir1/dir2 os.path.dirname(path2) = /dir1/dir2/file.txt os.path.basename(__file__) = some_test.py os.path.basename(path1) = file.txt os.path.basename(path2) = os.getcwd() = /home/hyg/code os.path.abspath(__file__) = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py os.path.abspath(path1) = /dir1/dir2/file.txt os.path.realpath(__file__) = /home/hyg/code/pytorch-study/vehicle-reid/some_test.py os.path.realpath(path1) = /dir1/dir2/file.txt os.path.split(__file__) = ('./pytorch-study/vehicle-reid', 'some_test.py') os.path.split(path1) = ('/dir1/dir2', 'file.txt') sys.path[0] = /home/hyg/code/pytorch-study/vehicle-reid sys.path[0] = /dir1/dir2/file.txt sys.argv[0] = ./pytorch-study/vehicle-reid/some_test.py