I am new to python and using python2.7 in linux, I wrote the small python program as below, but i want to store the each filename into one variable(index_list[1],...)
.
index_list=[]
index_list=commands.getoutput('find /etl/input/ -maxdepth 6 -iname "*tmp" ')
print index_list[1]
Thanks, Prasad
listdir() In Python, the os module provides a function listdir(dir_path), which returns a list of file and sub-directory names in the given directory path. Then using the filter() function create list of files only. Then sort this list of file names based on the name using the sorted() function.
numpy.save(file, array) array = numpy.load(file) Example 1: Save the Array to File In the following example: we will initialize an array; create and open a file in write binary mode; and then write the array to the file using numpy.save () method.
Python – List Files in a Directory. 1 Python. import os. path = " C://Users//Vanshi//Desktop//gfg ". dir_list = os.listdir (path) print("Files and directories in '", path, "' :") ... 2 Python3. 3 Python3. 4 Python3. 5 Python3. More items
Following is a quick code snippet where we use firstly use save () function to write array to file. Secondly, we use load () function to load the file to a numpy array. In the following example: we will initialize an array; create and open a file in write binary mode; and then write the array to the file using numpy.save () method.
Python has a set of built-in methods that you can use on lists/arrays. Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
Use os library: https://docs.python.org/2/library/os.html#os.listdir
import os
mylist = os.listdir(path)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With