保存到特定目录fi的通用路径名操作

2024-10-02 20:35:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个函数,在这个函数中我读inFile,以便将它分成两个文件(outFile1,outFile2)。在

我想要的是如果outFile1和/或outFile2没有路径名目录(例如:outFile1="result1.txt"outFile2="result2.txt"),那么这两个文件都与inFile(ex:inFile=“C:\mydata”)保存在同一个目录中\我的文件.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]

示例

^{2}$

这是我在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"

Tags: 文件pathname目录osextinfilefile
1条回答
网友
1楼 · 发布于 2024-10-02 20:35:28

像这样的怎么样?在

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.

相关问题 更多 >