2017-11-13 1 views
0

여러 개의 비 파이썬 사용자가 사용할 파이썬 스크립트를 작성 중입니다. 스크립트를 실행하는 데 필요한 매개 변수가 들어있는 텍스트 파일이 있습니다.파이썬에서 사용하기위한 텍스트 파일 내의 형식 경로

입력 중 하나는 경로입니다. 스크립트를 실행시킬 수 없어서 경로를 잘못 참조했기 때문이라고 생각했습니다. 내가 시도

은 :

C:\temp\test 
"C:\temp\test" 
r"C:\temp\test" 
C:/temp/test 
"C:/temp/test" 
C:\\temp\\test 
"C:\\temp\\test" 

나는 전화를 내 파이썬 스크립트에서 읽을 수있는 텍스트 파일에 다음 각 하나를 추가했습니다. 다른 매개 변수가 있는데 제대로 호출됩니다. 경로를 하드 코딩 할 때 스크립트가 실행되는 것 같습니다. 확인해야하는 몇 가지 버그가 있다고 생각하기 때문에 나타나는 것 같습니다.하지만 오류없이 실행됩니다. 나는 텍스트 파일을 사용하는 경우

는이 오류가 얻을 - 내가 위의 예 중 하나를 사용하는 경우에 따라 달라집니다 다음과 같이

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) 
+0

은 종종'\ n' caracter로 끝납니다. 필요 없으면 제거하는 것을 잊지 마십시오. – SRD

답변

1

당신의 오류 " '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]) 
+0

감사합니다! 나는 지금 그것을 시험하고있다. 내가 제공 한 예제 중 어느 것을 사용하겠습니까? –

+0

@KeaganAllan : "c : \ temp \ match1 \ jpg \ n /."처럼 보이는 한 줄만있는 경우 lstrip()을 시도하십시오. 정규식은 너무 빠르고 큰 데이터에 효율적입니다 (하지만 CPU가 높을 것입니다 ...)! – DRPK

+0

감사합니다. 나는 질문을 닫을 것이다. 도와 주신 모든 분들께 감사드립니다. 이 문제는 내가 깨닫지 못했던 다른 단계로 쏟아져 나왔고 그 작은 수정으로 내가 가진 모든 문제가 해결되었습니다. –

관련 문제