shardul kate
shardul kate

Reputation: 11

How to create a python code to convert all las files in user defined folder into respective excel file using las2excelbulk function

I have to convert .las files in one directory to .xlsx files using las2excelbulk function.

Currently I can do that in command prompt but I want to do it using Python: is it possible?

Here's the link which i referred https://lasio.readthedocs.io/en/latest/exporting.html

The file would be converted.

# this is working for only one file

import lasio

las = lasio.read('*.las')

las.to_excel('testsamplelas.xlsx')

Upvotes: 1

Views: 1221

Answers (1)

kii
kii

Reputation: 497

The argument to lasio.read() can only be a single filename, but you can use the os and fnmatch modules in the Python standard library to step through all .las files recursively.

import fnmatch
import os
import lasio

for root, dirnames, filenames in os.walk("your_directory"):
    for filename in fnmatch.filter(filenames, '*.las'):
        path = os.path.join(root, filename)
        las = lasio.read(path, ignore_header_errors=True)
        las.to_excel(path + ".xlsx")

lasio.read(..., ignore_header_errors=True) is the equivalent of las2excelbulk -i.

It may also be useful to inspect the code behind the las2excelbulk command line tool for more information.

Upvotes: 2

Related Questions