Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using File Extension Wildcards in os.listdir(path)

Tags:

python

I have a directory of files that I am trying to parse using Python. I wouldn't have a problem if they were all the same extension, but for whatever reason they are created with sequential numeric extensions after their original extension. For example: foo.log foo.log.1 foo.log.2 bar.log bar.log.1 bar.log.2 etc. On top of that, foo.log is in XML format, while bar.log is not. What's the best route to take in order to read and parse only the foo.log.* and foo.log files? The bar.log files do not need to be read. Below is my code:

import os from lxml import etree path = 'C:/foo/bar//' listing = os.listdir(path) for files in listing:     if files.endswith('.log'):         print files         data = open(os.path.join(path, files), 'rb').read()         tree = etree.fromstring(data)         search = tree.findall('.//QueueEntry') 

This doesn't work as it doesn't read any .log.* files and the parser chokes on the files that are read, but are not in xml format. Thanks!

like image 284
Dryden Long Avatar asked Nov 08 '12 20:11

Dryden Long


People also ask

What does OS Listdir (' ') mean?

listdir() method in python is used to get the list of all files and directories in the specified directory. If we don't specify any directory, then list of files and directories in the current working directory will be returned. Syntax: os.listdir(path)

How do I filter files with OS Listdir?

How to Filter and List Files According to Their Names in Python? To filter and list the files according to their names, we need to use “fnmatch. fnmatch()” and “os. listdir()” functions with name filtering regex patterns.

Does OS Listdir return a list?

Description. Python method listdir() returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.

Does OS Listdir include folder?

The listdir() method only lists all the top level files and folders inside a directory. If you want to navigate all the lower level files and folders inside your directory and subdirectories, use the walk() method from the OS module.


1 Answers

Maybe the glob module can help you:

import glob  listing = glob.glob('C:/foo/bar/foo.log*') for filename in listing:     # do stuff 
like image 75
stranac Avatar answered Sep 17 '22 12:09

stranac