I am going to assume these xyz files have the same headers as the first line. If there is no header in your xyz files, delete the lines indicated with #remove me if no header
in the code below. Place your xyz files in the same directory. Then you can iterate through the files with os.listdir
. From there use open
to open each file and write each line into a new file. In the script below, the first line of each xyz is skipped if it's not the first file.
inFolder
is the folder with your .xyz files.
outXyz
is the output file. if this file exists already, it will be overwritten.
# ---------- Locals
#input folder
inFolder = r"C:\Users\e1b8\Desktop\E1B8\GIS_Stackexchange"
#output file
#if file exists, file will be written over
outXyz = r"C:\Users\e1b8\Desktop\E1B8\GIS_Stackexchange\merge.xyz"
# ---------- End locals
#import modules
import os
#open out file for writing
outFile = open (outXyz, "w")
#Create list of xyz files in folder
xyzs = [xyz for xyz
in os.listdir (inFolder)
if xyz.lower().endswith (".xyz")]
#first variable for first line (header)
firstFile = True
#iterate xyz files
for xyz in xyzs:
#xyz file full path
xyzPath = os.path.join (inFolder, xyz)
#open xyz file
xyzFile = open (xyzPath, "r")
#second variable for first line (header)
firstLine = True
#iterate lines in file
for line in xyzFile:
#Skip header if first line of not-the-first file
if firstFile == False and firstLine == True: #remove me if no header
#switch variable to false - first line
firstLine = False #remove me if no header
continue #remove me if no header
#Write line to out file
outFile.write (line)
#switch varaible to false - not the first file
firstFile = False
#close xyz file
xyzFile.close ()
#close out file
outFile.close ()