What language to manipulate text files
Michael Hoffman
cam.ac.uk at mh391.invalid
Sun Jun 12 03:45:55 EDT 2005
ross wrote:
> I want to do some tricky text file manipulation on many files, but have
> only a little programming knowledge.
>
> What are the ideal languages for the following examples?
>
> 1. Starting from a certain folder, look in the subfolders for all
> filenames matching *FOOD*.txt Any files matching in each folder should
> be copied to a new subfolder within the current folder called EATING
> with a new name of *FOOD*COPY.txt
This should get you started:
import errno
from path import path # http://www.jorendorff.com/articles/python/path/
dst_dirpath = path("EATING")
# create dst_dirpath
try:
dst_dirpath.makedirs() # make destination directory and its parents
except OSError, err: # error!
if err.errno = errno.EEXIST: # might just be that it already exists
if not dst_dirpath.isdir(): # and it's a directory
raise # if not, raise an exception
for filepath in path(".").walkfiles("*FOOD*.txt"):
infile = file(filepath)
outfile = file(dst_dirpath.joinpath(filepath.namebase+"_COPY.txt"))
...do processing here...
> My first objective is to process the files as described.
> My second objective is to learn the best language for this sort of text
> manipulation. The language should run on Windows 98, XP and Linux.
>
> Would Python be best, or would a macro-scripting thing like AutoHotKey
> work?
Personally, I'd use Python, but what do you expect when you ask here?
--
Michael Hoffman
More information about the Python-list
mailing list