我正在编写一个供多个非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:[错误123]文件名,目录名称或卷标语法不正确:'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)I am writing a Python script for use by multiple non-Python users. I have a text file containing the parameters my script needs to run.
One of the inputs is a path. I cannot get my script to run and was thinking it was because I had referenced my path incorrectly.
I have tried:
C:\temp\test "C:\temp\test" r"C:\temp\test" C:/temp/test "C:/temp/test" C:\\temp\\test "C:\\temp\\test"I have added each one of these into a text file, which is called and read in my Python script. I have other parameters and they are called correctly, my script seems to run when I hard code the path in. I say seems because I think there are a few bugs I need to check, but it runs with no errors.
When I use the text file I get this error - which varies depending on if I used one of the above examples:
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'c:\temp\match1\jpg\n/.'
My code is as follows:
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])From your error "'c:\temp\match1\jpg\n/.'", i can see "\n" character, \n is symbole of new line ( when you press enter button ) you should remove that character from end of your path! did you try to do that? you can use .lstrip("\n") , replcae() or regx methods for remove that character.
Try to open and read line by line of your input file like this:
read_lines = [line.rstrip('\n') for line in open(r"C:\temp\Match1\Match_Table.txt")] print(read_lines) print(read_lines[1])更多推荐
发布评论