Iterating through a directory and assigning each file a variable to work with
<p>I'm really new to Python, so in advance thank you for this probably stupid question:</p>
<p>I have a directory filled with LAS files and I would like to assign each of the with a variable, so that I can continue working with them all afterwards. I can't seem to find an answer that works ... the code below, is for some reason I cant figure out not working - thanks for the help!</p>
<h1>%%</h1>
<pre><code>### Importing function packages ### NumPy, Pandas, PyPlot, OS, LASio, Sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import lasio as las


#%%
#Directory
my_dir = os.walk(&quot;C:\\Users\\L_R\\Desktop\\test\\&quot;)
my_list = []

#Creates a list with LAS files in the DIR
for i in my_dir:
    my_list.append(i)
print my_list

#Iterates through DIR and creates dataframes out of files

count=0
current_las=my_list[count]
filename = current_las

for dirname, dirs, files in my_dir:
    while count &lt; len(my_list):      
        if filename.endswith(&quot;.LAS&quot;):
            las_output = las.read(filename) #module reading the file
        count = count + 1
        
</code></pre>