码迷,mamicode.com
首页 > 编程语言 > 详细

Python获取当前路径

时间:2019-10-04 21:18:17      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:orm   element   search   bsp   environ   line   support   ecif   eth   

如下的代码列出了Python中获取当前路径的一些方法:

import os
import sys

def test():
    path1 = /dir1/dir2/file.txt
    path2 = /dir1/dir2/file.txt/

    print(__file__ =, __file__)

    # 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
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
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
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
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

Python获取当前路径

标签:orm   element   search   bsp   environ   line   support   ecif   eth   

原文地址:https://www.cnblogs.com/pursuiting/p/11623007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!