一句python代码
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)),
'lib'))
代码分析
本意是在sys.path列表中增加搜索.py文件的路径
这个路径是执行这个python代码的文件的目录的目录(比较拗口哦),如file的路径在/home/gwang/python/file,那么sys.path中就会增加/home/gwang/lib
比如一下代码(python_ospath.py):
import os
import sys
print(sys.path)
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)),'lib'))
print(sys.path)
结果如下:
~/gwang/python$ python python_ospath.py
['/home/gwang/python', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages']
['/home/gwang/lib', '/home/gwang/python', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages']
~/gwang$ python python/python_ospath.py
['/home/gwang/python', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages']
['/home/gwang/lib', '/home/gwang/python', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages']
sys.path路径中增加了'/home/gwang/lib',实际文件路径'/home/gwang/python/python_ospath.py',不过还可以看到sys.path也增加了执行文件的目录'/home/gwang/python/’
sys.path.insert
$ python
...
>>> import sys
>>> help(sys)
......
path -- module search path; path[0] is the script directory, else ''
>>> help(sys.path)
insert(self, index, object, /)
| Insert object before index.
sys.path 是指定模块搜索路径的字符串列表,可以使用print()函数查看,sys.path添加路径后,可以从添加的路径导入模块
还可以参考: sys.path.append
os.path.join
$ python
...
>>> import os
>>> help(os.path)
...
join(a, *p)
Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded. An empty last part will result in a path that
ends with a separator.
连接两个或多个路径名组件,根据需要插入“/”。
如果任何组件是绝对路径,则所有先前的路径组件将被丢弃。
最后一部分为空将导致路径以分隔符结尾。
os.path.join的示例代码(path_join.py)
import os
root="/"
a="a"
b="b"
c="/c"
d="d"
e="e.txt"
f=a+b+c+d+e
print(os.path.join(root,f))
print(os.path.join(root,a,b,c))
print(os.path.join(root,a,c,d,e))
print(os.path.join(root,a,d,e))
print(os.path.join(root,a,d,e,c))
说明:c路径比较特殊是绝对路径,所以前面的路径组件都会被丢弃,比如root,a,d,e,c的组合不是/a/d/e.txt//c而是直接为/c
输出结果如下:
$ python path_join.py
/ab/cde.txt
/c
/c/d/e.txt
/a/d/e.txt
/c
os.path.dirname
$ python
...
>>> import os
>>> help(os.path)
...
dirname(p)
Returns the directory component of a pathname
返回路径名的目录部分
示例代码
import os
import sys
if len(sys.argv) > 1:
print("dirname=", os.path.dirname(str(sys.argv[1])))
print("dirofdir=", os.path.dirname(os.path.dirname(str(sys.argv[1]))))
输出结果如下:
$ python python_dirname.py /123/d/
dirname= /123/d
dirofdir= /123
$ python python_dirname.py file.xt
dirname=
dirofdir=
$ python python_dirname.py /456/a/b/cfile.xt
dirname= /456/a/b
dirofdir= /456/a
$ python python_dirname.py /
dirname= /
dirofdir= /
$ python python_dirname.py .
dirname=
dirofdir=
参考文档
- Check and add the module search path with sys.path in Python | note.nkmk.me
- Python | os.path.join() method - GeeksforGeeks
- Python | os.path.dirname() method - GeeksforGeeks