7

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

0

2 Answers 2

20

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

import os

mylist = os.listdir(path)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Willem Van Onsem , but i want to use the find command in my program and output will be stored in array format. example my find command get the 4 filenames, each file name should be stored in separate variable like var[1],var[2]...etc
So save output from commands.getoutput('yourcommandhere') to variable and then split it by '\n' ex: index_list = index_list.split('\n')
4

you can try his

import os 
from pprint import pprint 

files = []
for dirname, dirnames, filenames in os.walk('.'):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        files.append(os.path.join(dirname, subdirname))

    # print path to all filenames.
    for filename in filenames:
        files.append(os.path.join(dirname, filename))


pprint(files)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.