常用方法 | 作用 |
os.path.dirname(__file__) | 返回当前python执行脚本的执行路径(看下面的例子),这里__file__为固定参数 |
os.path.abspath(file) | 返回一个文件在当前环境中的绝对路径,这里file 一参数 |
os.path.join(basedir,file) |
将file文件的路径设置为basedir所在的路径,这里fbasedir和file都为参数 |
1
2
3
4
|
xpleaf@leaf:~
/Source_Code
$
pwd
/home/xpleaf/Source_Code
xpleaf@leaf:~
/Source_Code
$
ls
hello.py test_os_path.py
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import
os
path1
=
os.path.dirname(__file__)
print
'The path1 is:'
, path1
path2
=
os.path.abspath(path1)
print
'The path2 is:'
, path2
path3
=
os.path.join(path2,
'hello.py'
)
print
'The path3 is:'
, path3
|
1
2
3
4
|
xpleaf@leaf:~
/Source_Code
$ python test_os_path.py
The path1 is:
The path2 is:
/home/xpleaf/Source_Code
The path3 is:
/home/xpleaf/Source_Code/hello
.py
|
1
2
3
4
|
xpleaf@leaf:~
/Source_Code
$ python
/home/xpleaf/Source_Code/test_os_path
.py
The path1 is:
/home/xpleaf/Source_Code
The path2 is:
/home/xpleaf/Source_Code
The path3 is:
/home/xpleaf/Source_Code/hello
.py
|
1
|
basedir
=
os.path.abspath(os.path.dirname(__file__))
|
1
|
static_file_path
=
os.path.join(basedir,
'index.html'
)
|