Re: [Tutor] How can I make a python script go directory by directory and excecute on files of choice
On 11/01/06, Liam Clarke <[EMAIL PROTECTED]> wrote: > Hi Srinivas - > > For walking a directory, you can use os.walk() or os.path.walk(), but > I prefer the path module here - > http://www.jorendorff.com/articles/python/path/. The Path module is excellent, but it's walk still doesn't take into account the depth of the current file in the folder structure. If you need that, I wrote (with Kent's help) a simple script that will take it into account (you need the Path module above for it to work). def traverse(directory, function, depth=0): import path thedir = path.path(directory) for item in thedir.files(): function(item, depth) for item in thedir.dirs(): traverse(item,function, depth+1) It can be used like: def doprint(item, depth): print item traverse(r"C:\Temp", doprint) Hope it's helpful to someone. Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How can I make a python script go directory by directory and excecute on files of choice
Hi Srinivas - For walking a directory, you can use os.walk() or os.path.walk(), but I prefer the path module here - http://www.jorendorff.com/articles/python/path/. As for determining if a file is really an .XLS format file or a tab delimited file with .xls on the end, go to www.wotsit.org, have a look at the .XLS specifications; there'll probably be a sequence of identifying bytes you can read to confirm that it's an XLS. Regards, Liam Clarke On 1/11/06, Srinivas Iyyer <[EMAIL PROTECTED]> wrote: > Dear group, > I have Excel files that are arranged according to > transctions under various name/directories. > > I found out that all these Excel files are not real > OLE based files and some of them are really tab delim > files with .XLS appended to the end of file name. I > got fooled and started using pyExcelator module. > > Now I want to write a script that can go to each > directory from base directory. > > Say: my dir path is like this: > > /home/srini/data/sales > - jan05/(1-31).xls > - feb05/(1-27).xls > - mar05/(1-31).xls > > How can I ask my script residing in data directory to > go to sales and scan all directories and report if > .xls files are really Excel files or text files with > .xls extension. > > could any one please help me here. > > thank you. > > > > > __ > Yahoo! DSL – Something to write home about. > Just $16.99/mo. or less. > dsl.yahoo.com > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How can I make a python script go directory by directory and excecute on files of choice
Dear group, I have Excel files that are arranged according to transctions under various name/directories. I found out that all these Excel files are not real OLE based files and some of them are really tab delim files with .XLS appended to the end of file name. I got fooled and started using pyExcelator module. Now I want to write a script that can go to each directory from base directory. Say: my dir path is like this: /home/srini/data/sales - jan05/(1-31).xls - feb05/(1-27).xls - mar05/(1-31).xls How can I ask my script residing in data directory to go to sales and scan all directories and report if .xls files are really Excel files or text files with .xls extension. could any one please help me here. thank you. __ Yahoo! DSL Something to write home about. Just $16.99/mo. or less. dsl.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor