2017-11-13 6 views
0

複数の非Pythonユーザーが使用するためのPythonスクリプトを作成しています。 私のスクリプトが実行する必要があるパラメータを含むテキストファイルがあります。Pythonで使用するためのテキストファイル内の書式パス

入力の1つがパスです。私のスクリプトを実行することができないと私は誤って私のパスを参照していたためだと思っていた。

私が試してみました:

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

私はPythonスクリプトで呼び出され、読み込まれたテキストファイルの中にこれらの各1を追加しました。 私は他のパラメータを持っていて、正しく呼び出されているので、パスをハードコードするとスクリプトが実行されているようです。私はチェックする必要のあるバグがいくつかあると考えているようです。

私はこのエラーを取得テキストファイルを使用する場合 - 私は上記の例のいずれかを使用した場合によって異なります。

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

あなたから"\ n"文字を見ることができます。\ nは、の改行コード(Enterボタンを押すと)の末尾からその文字を削除する必要があります。あなたのパスの!それをやろうとしましたか?その文字を削除するには、.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 /」のような1行だけあればlstrip()を試してください。正規表現は非常に速く、大量のデータに対して効率的です(しかし、CPUは高くなります...)! – DRPK

+0

ありがとう、これは働いた。私は質問を閉じてマークします。助けてくれた皆さん、ありがとう。この問題は、私が気づいていなかった他のステップや小さな修正によって、私が持っていたすべての問題をクリアしたことにも繋がりました。 –

関連する問題