Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store the file names in array from python

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

like image 398
Prasad Avatar asked Jan 23 '17 12:01

Prasad


People also ask

How do you sort files by name in python?

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.

How to save an array to a file in Python?

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.

How to list all files in a directory in Python?

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

How to write array to a NumPy array in Python?

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.

How do I use lists/arrays in Python?

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.


1 Answers

Use os library: https://docs.python.org/2/library/os.html#os.listdir

import os

mylist = os.listdir(path)
like image 130
How about nope Avatar answered Oct 06 '22 00:10

How about nope