python获取当前路径_Python获取当前路径

importosimportsysdeftest():

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值