python 获取运行python脚本所在目录
2016-01-16 01:01
537 查看
今天按照libsvm官方指导手册 使用libsvm 软件包。
本来应该按照指导,进入tools目录,然后执行命令
懒得切换工作目录,于是很自然,我执行命令
Traceback (most recent call last):
File “libsvm-3.21/tools/easy.py”, line 28, in
assert os.path.exists(svmscale_exe),”svm-scale executable not found”
AssertionError: svm-scale executable not found
仔细查看easy.py脚本,发现必须在tools目录下执行那条命令才能成功,于是我百度了,python如何获取执行脚本文件路径,大致如下
然后,我把
现在执行
本来应该按照指导,进入tools目录,然后执行命令
python easy.py svmguide1 svmguide1.t,可是我的训练文件和测试文件svmguide1和svmguide1.t都不在tools那个目录下。
懒得切换工作目录,于是很自然,我执行命令
python libsvm-3.21/tools/easy.py svmguide1 svmguide1.t,可是报错
Traceback (most recent call last):
File “libsvm-3.21/tools/easy.py”, line 28, in
assert os.path.exists(svmscale_exe),”svm-scale executable not found”
AssertionError: svm-scale executable not found
仔细查看easy.py脚本,发现必须在tools目录下执行那条命令才能成功,于是我百度了,python如何获取执行脚本文件路径,大致如下
Python 获取当前脚本文件路径目录 # -*- coding: cp936 -*- import sys,os #获取脚本文件的当前路径 def cur_file_dir(): #获取脚本路径 path = sys.path[0] #判断为脚本文件还是py2exe编译后的文件,如果是脚本文件,则返回的是脚本的目录,如果是py2exe编译后的文件,则返回的是编译后的文件路径 if os.path.isdir(path): return path elif os.path.isfile(path): return os.path.dirname(path)
然后,我把
easy.py脚本改为
path = sys.path[0] if os.path.isfile(path): path = os.path.dirname(path) print(path) pathdir = path is_win32 = (sys.platform == 'win32') if not is_win32: svmscale_exe = pathdir + "/../svm-scale" svmtrain_exe = pathdir + "/../svm-train" svmpredict_exe = pathdir + "/../svm-predict" grid_py = pathdir + "/grid.py" gnuplot_exe = "/usr/bin/gnuplot"
现在执行
python libsvm-3.21/tools/easy.py svmguide1 svmguide1.t,ok一切正常。
相关文章推荐
- python学习笔记-Day11--paramiko
- 举例讲解Python设计模式编程的代理模式与抽象工厂模式
- Python Numpy Tutorial
- [Python标准库]fractions——有理数
- [Python标准库]decimal——定点数和浮点数的数学运算
- python核心编程-sock例子
- python解析XML
- python安装Pillow
- python 爬虫(三)
- 【学神-RHEL7】P7-Python中函数的使用
- Python文件
- python--datetime,timedelta时间处理
- python学习第十课续 :线程池
- 《笨办法学Python》 第13课手记
- python 第十天
- python list循环语句的使用方法
- Python 文件读取与写入操作方法
- Python中函数的参数传递方式
- Python学习(三): 字符串操作
- python join字符连接函数的使用方法