1、获取工程所在根路径:根路径=os.path.dirname(os.path.abspath('__file__'))
2、将获取的根路径和相对路径组合:组合路径=os.path.join(根路径,相对路径)
3、规范化组合路径:os.path.normpath(组合路径)
通过以上三步骤就可以完全解决文件定位问题;
例子:
def edit_xmlfile(self,filepath,filename,flag,newvalue):
print u'--------------即将编辑xml文件--------------'
print u'输入参数为:',filepath,filename,flag,newvalue
filepathandfile=filepath+filename
print 'filepathandfile=',filepathandfile
#本地编辑文件
print 'projectrootpath=',os.path.dirname(os.path.abspath('__file__')) #工程(而非文件)所在根路径,精确到ZXJF/
joinpath=os.path.join(os.path.dirname(os.path.abspath('__file__')),filepathandfile)
normalpath=os.path.normpath(joinpath)
print 'normalpath=',normalpath
tree =parse(normalpath)
<