os.walk exclude.svn文件夹

2024-09-28 23:16:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个脚本,我想用它来更改整个项目文件夹结构中的重复字符串。一旦改变了,我就可以把它签入SVN。不过,当我运行脚本时,它会进入.svn文件夹,我希望它进入该文件夹。我怎样才能做到这一点?下面的代码,谢谢。

import os
import sys

replacement = "newString"
toReplace = "oldString"
rootdir = "pathToProject"


for root, subFolders, files in os.walk(rootdir):
  print subFolders
  if not ".svn" in subFolders:
    for file in files:
      fileParts = file.split('.')
      if len(fileParts) > 1:
        if not fileParts[len(fileParts)-1] in ["dll", "suo"]:
          fpath = os.path.join(root, file)
          with open(fpath) as f:
            s = f.read()
          s = s.replace(toReplace, replacement)
          with open(fpath, "w") as f:
            f.write(s)

print "DONE"

Tags: inimport脚本文件夹forifossvn
2条回答

试试这个:

for root, subFolders, files in os.walk(rootdir):
    if '.svn' in subFolders:
      subFolders.remove('.svn')

然后继续处理。

Err... what?

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again.

for root, subFolders, files in os.walk(rootdir):
  try:
    subFolders.remove('.svn')
  except ValueError:
    pass
  dosomestuff()

相关问题 更多 >