0

I have the following code to get a list of file names in a particular directory

import sys,os

data_files = [x[2] for x in os.walk(os.path.dirname(sys.argv[0]))]
print data_files

the output is something like

[['bar.py',  'foo.py', 'foo.pyc', 'fooBar', 'fooBar.py', 'tar.py', 'tar.pyc']]

I want to parse this list and pass only a particular filename to another function

for example : I want to parse the list?( is it a list?) above and pass only tar.py to another function , ie the name tar , so as another function can use it like

import filename ( in this case tar)

I am new to python and tried a lot of list parsing stuff but could not extract the name . any help would be appreciated

I am using python 2.7

Thank you for your time

EDIT:

I figured out how to parse the list

data_files = [x[2] for x in os.walk(os.path.dirname(sys.argv[0]))]
hello = data_files[0]
print hello[0].split(".")[0]

the problem is when I try assigning the file name to a variable like

var = hello[0].split(".")[0]

and use import var

but I am not sure if python allows importing modules like this because it does not consider var as a variable but a module name . how can I overcome this

4
  • I want to get the file name , for ex : the list comes out as [['bar.py']] , I want the filename to be just bar , so as an import statement such as "import (filename)" works Commented Nov 27, 2012 at 20:15
  • >"bar.py".split(".")[0] >"bar" Commented Nov 27, 2012 at 20:19
  • thank you for your answer , but how do I parse the list ,I just cnt seem to do it with normal list parsing Commented Nov 27, 2012 at 20:21
  • What do you mean by normal list parsing? Commented Nov 27, 2012 at 20:24

2 Answers 2

2

I'm a little confused on what you mean by list parsing. It is a list and if you want to remove all the file extensions you can do it with a list comprehension and split as fastreload suggested.

new_list = [x.split('.')[0] for x in data_files]

If you need to step through the list looking at each file you can do that with

for file in data_files:
    your code goes here
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I accidentally edited your post when I was supposed to edit mine.
0

If you know which index your element is at, you can do file_list[index_of_your_element].split('.')[0], for example:

file_list = ['bar.py',  'foo.py', 'foo.pyc', 'fooBar', 'fooBar.py', 'tar.py', 'tar.pyc']
print l[5].split('.')[0] == 'tar'

Will print true. Your question is kinda confusing, what do you know about the element, and how do you want to use it?

2 Comments

hi , Thank you for your answer , I used the following code to parse the list data_files = [x[2] for x in os.walk(os.path.dirname(sys.argv[0]))] hello = data_files[0] print hello[0].split(".")[0] so I get the name of the file I want , but the problem is this file name is assigned to a variable , hence import var will not look for the variable but var . hence I cannot import the file I want
Use __import__(module_name)

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.