I want to walk a directory and ignore all the files or directories which names begin in '.' (e.g. '.svn').
Then I will process all the files.
My test program walknodot.py does not do the job yet.
Please help.

#!c:/Python31/python.exe -u
import os

path = "C:\\test\\com.comp.hw.prod.proj.war\\bin"
for dirpath, dirs, files in os.walk(path):
    print (dirpath)
    print (dirs)
    print (files)
    for filename in files:
        print ("filename " + filename)
        print (dirs)
        if "^.*" in dirs:
            continue
        if "^.*" in filename:
            continue
        print ("no . ")
        # process the files here

C:\python>walknodot.py
C:\test\com.comp.hw.prod.proj.war\bin

[]
C:\test\com.comp.hw.prod.proj.war\bin\.svn


filename entries

no .
filename entries.bak

no .
C:\test\com.comp.hw.prod.proj.war\bin\.svn\prop-base
[]
[]
C:\test\com.comp.hw.prod.proj.war\bin\.svn\props
[]
[]
C:\test\com.comp.hw.prod.proj.war\bin\.svn\text-base
[]
[]
C:\test\com.comp.hw.prod.proj.war\bin\.svn\tmp

[]
C:\test\com.comp.hw.prod.proj.war\bin\.svn\tmp\prop-base
[]
[]
C:\test\com.comp.hw.prod.proj.war\bin\.svn\tmp\props
[]
[]
C:\test\com.comp.hw.prod.proj.war\bin\.svn\tmp\text-base
[]
[]
C:\test\com.comp.hw.prod.proj.war\bin\com

[]
C:\test\com.comp.hw.prod.proj.war\bin\com\comp

[]
C:\test\com.comp.hw.prod.proj.war\bin\com\comp\hw

[]
C:\test\com.comp.hw.prod.proj.war\bin\com\comp\hw\proj

[]
C:\test\com.comp.hw.prod.proj.war\bin\com\comp\hw\proj\war

['Activator.class', 'MessageReceiver.class', 'messages.properties', 'messages_fr.properties', 'ShutDownController.class', 'HwController
.class', 'HwManagerController.class']
filename Activator.class

no .
filename MessageReceiver.class

no .
filename messages.properties

no .
filename messages_fr.properties

no .
filename ShutDownController.class

no .
filename HwController.class

no .
filename HwManagerController.class

no .
C:\test\com.comp.hw.prod.proj.war\bin\com\comp\hw\proj\war\service


filename HwService.class

no .
C:\test\com.comp.hw.prod.proj.war\bin\com\comp\hw\proj\war\service\impl
[]

filename HwServiceImpl.class
[]
no .
filename HwServiceManager.class
[]
no .

Recommended Answers

All 5 Replies

Have you tried singling out the first character of the filename?
For example:

#!c:/Python31/python.exe -u
import os

path = "C:\\test\\com.comp.hw.prod.proj.war\\bin"
for dirpath, dirs, files in os.walk(path):
    print (dirpath)
    print (dirs)
    print (files)
    for filename in files:
        [B]if filename[0] != ".":[/B]
            print ("filename " + filename)
        print (dirs)
        if "^.*" in dirs:
            continue
        if "^.*" in filename:
            continue
        print ("no . ")
        # process the files here

I rewrite my program to:

#!c:/Python31/python.exe -u
import os
import re

path = "C:\\test\\com.comp.hw.prod.proj.war\\bin"
for dirpath, dirs, files in os.walk(path):
    print ("dirpath " + dirpath)
    p = re.compile('\\\.(\w)+$')
    if p.match(dirpath):
        continue
    print ("dirpath " + dirpath)
    for dir in dirs:
        print ("dir " + dir)
        if dir.startswith('.'):
            continue

        print (files)
        for filename in files:
            print ("filename " + filename)
            if filename.startswith('.'):
                continue
            print ("no . ")
            print ("dirpath filename " + dirpath + "\\" + filename)
    	    # process the files here

C:\python>walknodot.py
dirpath C:\test\com.comp.hw.prod.proj.war\bin
dirpath C:\test\com.comp.hw.prod.proj.war\bin
dir .svn
dir com
[]
dirpath C:\test\com.comp.hw.prod.proj.war\bin\.svn
dirpath C:\test\com.comp.hw.prod.proj.war\bin\.svn

I do not expect C:\test\com.comp.hw.prod.proj.war\bin\.svn to appear twice.
Please help.

It was tough to understand your code so I almost rewrote it to my way, this program output indented directorys marked with newline and *s and walks subdirectorys with more indention recursively. This is nice place to use recursion in my opinion.

#!c:/Python31/python.exe -u
import os

def mywalk(path,depth=0):
    if path.startswith('.'):
        return
    for dirpath, dirs, files in os.walk(path):
        print ()
        print (" " * depth + "*" + dirpath + "*")
        for dirname in dirs:
            mywalk(os.path.join(dirpath,dirname),depth=depth+3)
        for filename in [f for f in files if not f.startswith('.')]:
            if os.path.exists(os.path.join(dirpath,filename)): ## debug check
                if filename.startswith('.'):
                    continue
                print (" " * depth + filename) ## full filename too long to print here
                # process the files here
            else:
                raw_input(os.path.join(dirpath,filename)+' error') ## debug check

mywalk("/Python31")

for Python 2.6 add

from __future__ import print_function

don't forget that, in os.walk, if you remove a dir from "dirs", it's subtree won't be explored.
So

for dirpath, dirs, files in os.walk(path):
    for d in dirs:
        if d.startswith('.'):
            dirs.remove(d)  # d's subtree won't be explored
    for f in files:
        if not f.startswith('.'):
            process_file()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.