在 Python 中使用文本文件中的格式路径
Format path within Text File for consumption in Python
我正在编写一个 Python 脚本供多个非 Python 用户使用。
我有一个文本文件,其中包含我的脚本需要 运行.
的参数
输入之一是路径。我无法将我的脚本发送到 运行,并认为这是因为我错误地引用了我的路径。
我试过:
C:\temp\test
"C:\temp\test"
r"C:\temp\test"
C:/temp/test
"C:/temp/test"
C:\temp\test
"C:\temp\test"
我已将其中的每一个都添加到一个文本文件中,该文件在我的 Python 脚本中被调用和读取。
我有其他参数,它们被正确调用,当我硬编码路径时,我的脚本似乎 运行。我说似乎是因为我认为我需要检查一些错误,但它 运行没有错误。
当我使用文本文件时出现此错误 - 这取决于我是否使用上述示例之一:
WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect: 'c:\temp\match1\jpg\n/.'
我的代码如下:
print ("Linking new attachments to feature")
fp = open(r"C:\temp\Match1\Match_Table.txt","r") #reads my text file with inputs
lines=fp.readlines()
InFeat = lines[1]
print (InFeat)
AttFolder = lines[3] #reads the folder from the text file
print (AttFolder)
OutTable = lines[5]
if arcpy.Exists(OutTable):
print("Table Exists")
arcpy.Delete_management(OutTable)
OutTable = lines[5]
print (OutTable)
LinkF = lines[7]
print (LinkF)
fp.close()
#adding from https://community.esri.com/thread/90280
if arcpy.Exists("in_memory\matchtable"):
arcpy.Delete_management("in_memory\matchtable")
print ("CK Done")
input = InFeat
inputField = "OBJECTID"
matchTable = arcpy.CreateTable_management("in_memory", "matchtable")
matchField = "MatchID"
pathField = "Filename"
print ("Table Created")
arcpy.AddField_management(matchTable, matchField, "TEXT")
arcpy.AddField_management(matchTable, pathField, "TEXT")
picFolder = r"C:\temp\match1\JPG" #hard coded in
print (picFolder)
print ("Fields added")
fields = ["MatchID", "Filename"]
cursor = arcpy.da.InsertCursor(matchTable, fields)
##go thru the picFolder of .png images to attach
for file in os.listdir(picFolder):
if str(file).find(".jpg") > -1:
pos = int(str(file).find("."))
newfile = str(file)[0:pos]
cursor.insertRow((newfile, file))
del cursor
arcpy.AddAttachments_management(input, inputField, matchTable, matchField, pathField, picFolder)
从你的错误“'c:\temp\match1\jpg\n/.'”,我可以看到“\n”字符,\n 是 换行 的符号(当你按下回车键时)你应该从路径的末尾删除该字符!你尝试这样做了吗?您可以使用 .lstrip("\n") 、 replcae() 或 regx 方法删除该字符。
尝试像这样打开并逐行读取输入文件:
read_lines = [line.rstrip('\n') for line in open(r"C:\temp\Match1\Match_Table.txt")]
print(read_lines)
print(read_lines[1])
我正在编写一个 Python 脚本供多个非 Python 用户使用。 我有一个文本文件,其中包含我的脚本需要 运行.
的参数输入之一是路径。我无法将我的脚本发送到 运行,并认为这是因为我错误地引用了我的路径。
我试过:
C:\temp\test
"C:\temp\test"
r"C:\temp\test"
C:/temp/test
"C:/temp/test"
C:\temp\test
"C:\temp\test"
我已将其中的每一个都添加到一个文本文件中,该文件在我的 Python 脚本中被调用和读取。 我有其他参数,它们被正确调用,当我硬编码路径时,我的脚本似乎 运行。我说似乎是因为我认为我需要检查一些错误,但它 运行没有错误。
当我使用文本文件时出现此错误 - 这取决于我是否使用上述示例之一:
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'c:\temp\match1\jpg\n/.'
我的代码如下:
print ("Linking new attachments to feature")
fp = open(r"C:\temp\Match1\Match_Table.txt","r") #reads my text file with inputs
lines=fp.readlines()
InFeat = lines[1]
print (InFeat)
AttFolder = lines[3] #reads the folder from the text file
print (AttFolder)
OutTable = lines[5]
if arcpy.Exists(OutTable):
print("Table Exists")
arcpy.Delete_management(OutTable)
OutTable = lines[5]
print (OutTable)
LinkF = lines[7]
print (LinkF)
fp.close()
#adding from https://community.esri.com/thread/90280
if arcpy.Exists("in_memory\matchtable"):
arcpy.Delete_management("in_memory\matchtable")
print ("CK Done")
input = InFeat
inputField = "OBJECTID"
matchTable = arcpy.CreateTable_management("in_memory", "matchtable")
matchField = "MatchID"
pathField = "Filename"
print ("Table Created")
arcpy.AddField_management(matchTable, matchField, "TEXT")
arcpy.AddField_management(matchTable, pathField, "TEXT")
picFolder = r"C:\temp\match1\JPG" #hard coded in
print (picFolder)
print ("Fields added")
fields = ["MatchID", "Filename"]
cursor = arcpy.da.InsertCursor(matchTable, fields)
##go thru the picFolder of .png images to attach
for file in os.listdir(picFolder):
if str(file).find(".jpg") > -1:
pos = int(str(file).find("."))
newfile = str(file)[0:pos]
cursor.insertRow((newfile, file))
del cursor
arcpy.AddAttachments_management(input, inputField, matchTable, matchField, pathField, picFolder)
从你的错误“'c:\temp\match1\jpg\n/.'”,我可以看到“\n”字符,\n 是 换行 的符号(当你按下回车键时)你应该从路径的末尾删除该字符!你尝试这样做了吗?您可以使用 .lstrip("\n") 、 replcae() 或 regx 方法删除该字符。
尝试像这样打开并逐行读取输入文件:
read_lines = [line.rstrip('\n') for line in open(r"C:\temp\Match1\Match_Table.txt")]
print(read_lines)
print(read_lines[1])