Python: Store file names in an array or something

Hi,

I have just started learning Python and i am working on a code in which it list all the files in a directory of .doc extension.

Here is the code



#The code is run by defining the path at the command line argument i.e.
# python test.py /home/get/Desktop/python

import os
import sys

print "command line argument is", sys.argv[1]
#path = "/home/gt/Desktop/python"
for file in [doc for doc in os.listdir(sys.argv[1]) if doc.endswith(".doc")]:


	print file


Now it prints the names of the files which have .doc extension but i want to store all those file names in an array or some kind of variable. But i don’t know how to save all this information and stuck here in Python.

Because once i get these filenames stored in a kind of array then i need read these files.

Please Help.

usmangt

Hi,

I have somehow worked around with the array, but now i am getting an error when i am about to finish.

What i have done now is that i have initialized a blank array and storing the file names in that array. and now it stores all the names.

Now i am trying to read the lines written in those files which i have stored in the array but their i am getting the error.

I have highlighted the error with comments in the script.



file_list = []

print "command line argument is", sys.argv[1]
#path = "/home/gt/Desktop/python"
for file in [doc for doc in os.listdir(sys.argv[1])
if doc.endswith(".doc")]:

	file_list.append(file)

for i in range(len(file_list)):
#Works fine till here when printing the file names
	print file_list[i]

#But getting error here
# Says IOError: [Errno 2] No such file or directory: 'file_list[0]'
	f = open(r'file_list[i]')
	
	for j in range(2):
		print str(j) + ': ' + f.readline(),
	f.close()


Please Help

I’m not sitting in front of a 'puter w/ python on it, plus I’m fairly green at this stuff myself. But this bit:


#But getting error here
# Says IOError: [Errno 2] No such file or directory: 'file_list[0]'
    f = open(r'file_list[i]')

I think its complaining because file_list is the name of your array (list), and you are telling to open the list as a file - which doesn’t work. Plus it looks like your file open syntax is a little off as well. What you want (I think) is to pull the file names from the list, and present them to the file open command like this:


for file in file_list:
    f = open(file, 'r')

No guarantees, as I’m shootin’ from the hip here (and looking at the python docs as I go). :wink:

Not quite sure I grok what you’re trying to do with the last bit of code, though.

HTH,

Monte

Yes, That works fine, Thanks for that :slight_smile:

Also.

As you can see in my script that i am taking command line arguments (which is that path to scan the doc files)

i am trying to apply try and except on it, So that if the user don’t pass the CLI then it handles the situation. but its not working well.


try:
	for file in [doc for doc in os.listdir(sys.argv[1]) 
	if doc.endswith(".doc")]:

		file_list.append(file)

		for files in file_list:
			f = open(file,'r')
			

#	for i in range(len(file_list)):

#        	print "The contents of file", file_list[i], "are:"
#		print " "
#		f = open(file_list[i],'r')

			for j in range(2):
				print str(j) + ': ' + f.readline(),
			f.close()
			print " "
except IOError:
	handle_error()
except IOError, (errno, strerror):
	print "I/O error(%s): %s" % (errno, strerror)
except ValueError:
	print "Could not convert data to an integer."
except:
	print "Unexpected Error:", sys.exc_info()[0]
	raise

so, the errors comes at this line

	for file in [doc for doc in os.listdir(sys.argv[1])  

and then the try and except fails to handle the exception.

please help.

//usman

In that line, you have a few syntax errors: you left off a bracket and the colon. Your subsequent indenting needs to be fixed as well.


for file in [doc for doc in os.listdir(sys.argv[1])]:

However, I think you could just do this to simply things:


for file in os.listdir(sys.argv[1]):

There is no reason to loop through the directory’s files twice, if you’re not performing some type of operation in the process.

Hi, this is an old question but I am just trying to answer it. I’ve just joined sitepoint.
In python, if you want to iterate through a list you only need to mention the list in a ‘for’ statement. You don’t need to use range(…). So this is your code, with some corrections to make it work.

import sys, os

file_list = []
 
print "command line argument is", sys.argv[1]
#path = "/home/gt/Desktop/python"
for file in [doc for doc in os.listdir(sys.argv[1]) 
if doc.endswith(".xml")]:
    file_list.append(file)
 
for i in file_list:
    print i
    f = open(i, 'r')    
    for j in range(2):
        print str(j) + ': ' + f.readline(),
    f.close()

Please note the for statement: for i in file_list.

pramkriuk,

Note the first for-loop. You repeated the errors that ethanp pointed out, namely syntax errors and a list comprehension that does nothing.