1
我正在寫一個函數,我讀爲了將它分成兩個文件(outFile1,outFile2)。這兩個文件保存在同一目錄(例如:INFILE =「C:\ MYDATA \ myfile.txt的:通用路徑名操作保存在一個特定的目錄文件
如果outFile1
和/或outFile2
都沒有路徑目錄(outFile1="result1.txt"
和outFile2="result2.txt"
前)指定我要的是「)。如果輸出文件的路徑名目錄存在,我希望將結果保存在該目錄中。
當我沒有報告outFile
路徑名目錄時,這些文件被保存在與我的python腳本相同的目錄中。
def LAS2LASDivide(inFile,outFile1,outFile2,Parse,NumVal):
inFile_path, inFile_name_ext = os.path.split(os.path.abspath(inFile))
outFile1_path, outFile1_name_ext = os.path.split(os.path.abspath(outFile1))
outFile2_path, outFile2_name_ext = os.path.split(os.path.abspath(outFile2))
outFile1_name = os.path.splitext(outFile1_name_ext)[0]
outFile2_name = os.path.splitext(outFile2_name_ext)[0]
例如
inFile="C:\\mydoc\\Area_18.las"
outFile1="Area_18_overlap.las"
outFile2="Area_18_clean.las"
inFile_path, inFile_name_ext = os.path.split(os.path.abspath(inFile))
inFile_path, inFile_name_ext
('C:\\mydoc', 'Area_18.las')
outFile1_path, outFile1_name_ext = os.path.split(os.path.abspath(outFile1))
outFile1_path, outFile1_name_ext
('C:\\Program Files\\PyScripter', 'Area_18_overlap.las')
這是我所有代碼(測試)與mgilson
import os
from os import path
from liblas import file as lasfile
def LAS2LASDivide(inFile,outFile1,outFile2,Parse,NumVal):
inFile_path, inFile_name_ext = os.path.split(os.path.abspath(inFile))
outFile1_path, outFile1_name_ext = os.path.split(os.path.abspath(outFile1))
outFile2_path, outFile2_name_ext = os.path.split(os.path.abspath(outFile2))
outFile1_name = os.path.splitext(outFile1_name_ext)[0]
outFile2_name = os.path.splitext(outFile2_name_ext)[0]
if outFile1_name != outFile2_name:
# function pesudo_switch
def pseudo_switch(x):
return {
"i": p.intensity,
"r": p.return_number,
"n": p.number_of_returns,
"s": p.scan_direction,
"e": p.flightline_edge,
"c": p.classification,
"a": p.scan_angle,
}[x]
h = lasfile.File(inFile,None,'r').header
# change the software id to libLAS
h.software_id = ""
if not os.path.split(outFile1)[0]:
file_out1 = lasfile.File(os.path.abspath("{0}\\{1}.las".format(inFile_path,outFile1_name)),mode='w',header= h)
else:
file_out1 = lasfile.File(os.path.abspath("{0}\\{1}.las".format(outFile1_path,outFile1_name)),mode='w',header= h)
if not os.path.split(outFile2)[0]:
file_out2 = lasfile.File(os.path.abspath("{0}\\{1}.las".format(inFile_path,outFile2_name)),mode='w',header= h)
else:
file_out2 = lasfile.File(os.path.abspath("{0}\\{1}.las".format(outFile2_path,outFile2_name)),mode='w',header= h)
for p in lasfile.File(inFile,None,'r'):
if pseudo_switch(Parse) == int(NumVal):
file_out1.write(p)
elif pseudo_switch(Parse) != int(NumVal):
file_out2.write(p)
file_out1.close()
file_out2.close()
else:
print "outFile1 and outFile2 cannot have the same name"
在幾分鐘內感謝我將測試您的解決方案 –
我測試了您的代碼,我修改了我的舊代碼 –