Python file

So I have a working solution, but it is ugly and seems non-idiomatic. The problem is this:

For a directory tree, where each directory is configured as follows:

  • 1 .xc file
  • at least 1 .x file
  • any number of directories that match the same format

and nothing more. I would like, given the root path, to go through the tree, applying xc() to the contents of the .xc fies, x to the contents of the .x files, and then do the same with the contents of the child folders.

The actual code with explanation will be appreciated.

Thanks!

+7
source share
4 answers

The os.walk function recursively scans the directory tree, returning all file and subdirectory names.

So, all you have to do is find the .x and .xc from the file names and apply your functions when they do it (unverified code follows):

 import os for dirpath, dnames, fnames in os.walk("./"): for f in fnames: if f.endswith(".x"): x(os.path.join(dirpath, f)) elif f.endswith(".xc"): xc(os.path.join(dirpath,f)) 

It is suggested that x and xc can be caused by file names; alternately, you can first read the contents and pass this as a string for functions.

+17
source
 import os # your functions def xc(contents): .... def x(contents): .... # store function references in a dict to dispatch, this is a common python idiom funcMap = {'.xc': xc, '.x':x} for dirpath, dirnames, filenames in os.walk(someDir): # use os.walk to iterate someDir contents recursively. No # need to implement recursion yourself if stdlib does it for you for f in filenames: ext = os.path.splitext(f)[1] try: function = funcMap[ext] except KeyError: # no function to process files with extension 'ext', ignore it pass else: abspath = os.path.join(dirpath, f) with open(abspath) as f: function(f.read()) 
+6
source

Looks like this is a good place to use recursion:

 import os def process_directory(dirName): """Recursively process all .xc and .x files in a parent directory and its subdirectories""" dirName = os.path.abspath(dirName) for f in os.listdir(dirName): f = os.path.join(dirName, f) baseName, ext = os.path.splitext(f) if ext == '.xc': print "Processing [", f, "]" xc(f) elif ext == '.x': print "Processing [", f, "]" x(f) elif os.path.isdir(f): print "\nDescending into directory", f process_directory(dirName=os.path.join(dirName, f)) else: print "Not processing", f 

I hope I have not missed the point of your question.

+1
source

You can use dict to save the extension -> function:

 funcMap = {".xc" : xc, ".x" : x} 

Then you create a recursive function that takes one directory, gets a list of files in that directory, and determines the extension of each file:

 def iterateDir(s): l = dir.list(s) # Not a real function! for entry in l: ext = entry.extension() # Not a real function! 

Now in this for loop, you need to determine if the entry is a file or directory, and do the right thing:

  if isdir(entry): iterateDir(entry) elif ext in funcMap.keys(): funcMap[ext]() 

This should work for what you want to do.

Disclaimer - cannot promise that all of this is truly Python. This is basically psuedocode with Python type syntax. You should be able to get an idea of ​​what to do with it, though.

-2
source

All Articles