简体   繁体   English

格式化文本文件中的路径以供Python使用

[英]Format path within Text File for consumption in Python

I am writing a Python script for use by multiple non-Python users. 我正在编写一个供多个非Python用户使用的Python脚本。 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. 我已将其中的每一个添加到一个文本文件中,该文件在我的Python脚本中被调用和读取。 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/ . WindowsError:[错误123]文件名,目录名称或卷标签语法不正确:'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)

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! 从错误“'c:\\ temp \\ match1 \\ jpg \\ n /。'”中,我可以看到“ \\ n”字符,\\ n是换行符 (当您按Enter键时),应从末尾删除该字符你的路! did you try to do that? 您尝试这样做了吗? you can use .lstrip("\\n") , replcae() or regx methods for remove that character. 您可以使用.lstrip(“ \\ n”),replcae()或regx方法删除该字符。

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM