Python获取当前路径
生活随笔
收集整理的這篇文章主要介紹了
Python获取当前路径
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
如下的代碼列出了Python中獲取當(dāng)前路徑的一些方法:
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()
代碼腳本所在的絕對(duì)路徑為:
/home/hyg/code/pytorch-study/vehicle-reid/some_test.py
執(zhí)行如下命令:
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
執(zhí)行如下命令:
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
執(zhí)行如下命令:
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
執(zhí)行如下命令:
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
總結(jié)
以上是生活随笔為你收集整理的Python获取当前路径的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 椰子粉的功效!(椰子粉的功效和作用有哪些
- 下一篇: 传祺gs8四驱能切换两驱吗?