Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:06:34+00:00 2026-06-13T22:06:34+00:00

I am writing a function where I read inFile in order to split it

  • 0

I am writing a function where I read inFile in order to split it into two files (outFile1,outFile2).

What I want is if outFile1 and/or outFile2 are specified without pathname directory (ex: outFile1="result1.txt" and outFile2="result2.txt") both files are saved in the same directory as inFile (ex: inFile=”C:\mydata\myfile.txt”). If the pathname directory for the output files is present, I wish to save the results in that directory.

when I don’t report the the outFile pathname directory, the files are saved in the same directory as my python script.

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]

example

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

this is all my code (tested) modify with the suggestion of 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"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:06:35+00:00Added an answer on June 13, 2026 at 10:06 pm

    What about something like this?

    def new_path(fcheck,fpath):
        """
        fcheck --> filename to check
        fpath  --> file with path component to transfer 
                   if fcheck has no path component
        """
        head,tail = os.path.split(fcheck)
        return os.path.join(os.path.split(fpath)[0],tail) if not head else fcheck
    
    new_path('foo/bar','baz/qux')   #'foo/bar'  -- has path component.  Leave alone
    new_path('bar','baz/qux')       #'baz/bar'  -- No path component.  Transfer
    new_path('bar','qux')           #'bar'      -- Neither has path component.  Path must be '.'.  Leave alone.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a generator function. I want to know if there's a better (read:
I am writing a function in which I need to read a string contains
I'm writing an asynchronous read callback function and as I'm learning C# at the
I'm writing simple function witch will read data from myfile.txt with fgets(). Content of
I am writing a function which would get all files from a particular directory
I'm writing a PHP function that will delete all the files in the directory
I am writing a program that should read in a number of csv files
I am writing a generic test function that will accept a function address (read
I am writing function that involve other function from base R with a lot
I'm currently writing a function what would create a zip file, which will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.