Pythonos.path.exists变量问题

2024-10-01 13:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试运行一个python模块,它搜索一个表,并从该表中的一些cels中提取数据(使用变量“layerxpression”)。在某些情况下,该单元格包含另一个python文件的路径名(例如,表单元格中可能有以下路径名:'C:\Users\me\Documents\Working\PyFiles\示例.py'). 我的python程序将每个单元格的值赋给变量“CommentsExpression”,然后检查变量,看它是否确实引用了现有文件的路径名(使用os.path.exists()). 如果是这样,那么我的程序将把另一个python文件作为模块导入,并从中提取特定的变量——在本例中是变量“expression”。在

我的问题是,当我从表中提取路径名时,将其分配给变量“CommentsExpression”并运行它os.path.exists(),它总是显示为false——即使文件路径确实存在。我尝试过使用r'[path name]',但是没有幸运作为变量。下面是我的代码示例。在

import arcpy, os, re, array, sys, glob
from arcpy import env

CommentsExpression = ''
LayerExpression = '"Database - Fish Species"'
rows = arcpy.SearchCursor(r'C:\Users\me\Documents\Working\PyFiles\Master_Table.py')
for row in rows:
LayerField = row.getValue("Layer")
if LayerField == LayerExpression:
    CommentsExpression = CommentsExpression = row.getValue(str("Comments"))
    print os.path.exists(CommentsExpression)
    CommentsExpressionOutput = os.path.basename(CommentsExpression)
    CommentsExpressionOutput = CommentsExpressionOutput.split('.')
    CommentsExpressionOutput = str(CommentsExpressionOutput[0])
    if os.path.exists(CommentsExpression) == True:
        print 'True'
        pyFile = __import__(CommentsExpressionOutput)
        print pyFile.codeblock
    else:
        print 'False'

Tags: 模块文件pathimportosexistsusersdocuments