2012-12-25 41 views
-1

我有一个基本的问题(如下#1)和一个问题要问,我不知道对答案(#2)。任何人都可以提供意见如何限制搜索的文件只有特定的扩展

1.How将搜索范围限制到只有特定的extensions,可以说只有.c.h.cpp

2.How使点".""\n"之前在下面可选

userstring="Copyright (c) 2012 Company, Inc.\nAll Rights Reserved.\nCompany Confidential and Proprietary." variable 

import os 
import sys 
import fnmatch 
userstring="Copyright (c) 2012 Company, Inc.\nAll Rights Reserved.\nCompany Confidential and Proprietary." 
print len(sys.argv) 
print sys.argv[1] 
if len(sys.argv) < 2: 
    sys.exit('Usage: python.py <build directory>') 
for r,d,f in os.walk(sys.argv[1]): 
    for files in f: 
     userlines = userstring.split('\n') # Separate the string into lines 
     if files.endswith("." + c) or files.endswith("." + cpp): 
      with open(os.path.join(r, files), "r") as file: 
       match = 0 
       for line in file: 
        if userlines[match] in line.strip('\n\r .'): # Check if the line at index `m` is in the user lines 
         match += 1 # Next time check the following line 
        elif match > 0: # If there was no match, reset the counter 
         match = 0 
        if match >= len(userlines): # If 3 consecutive lines match, then you found a match 
         break 
       if match != len(userlines): # You found a match 
        print files 
usertring变量

编译错误: -

File "test.py", line 12, in <module> 
    if files.endswith("." + c) or files.endswith("." + cpp): 
NameError: name 'c' is not defined 
+2

看看了'glob'模块。 –

+0

不确定您的问题中的代码缩进是否正确。 – Paolo

+0

@AshwiniChaudhary - 根据以下建议使用endswith,但遇到编译错误 – user1927233

回答

0

然后fnmatch模块用于测试针对模式匹配文件名。

正则表达式可以帮助你搜索的内容匹配的变化。

import os 
import sys 
import re 
import fnmatch 

# Build a match pattern with optional periods and any amount of whitespace 
# between the sentences. 
userstring = re.compile(r"Copyright \(c\) 2012 Company, Inc\.?\sAll Rights Reserved\.?\sCompany Confidential and Proprietary\.?") 

print len(sys.argv) 
print sys.argv[1] 
if len(sys.argv) < 2: 
    sys.exit('Usage: python.py <build directory>') 
for path,dirs,files in os.walk(sys.argv[1]): 
    for fname in files: 
     # Test the filename for particular pattern matches. 
     for pat in ['*.cpp','*.c','*.h']: 
      if fnmatch.fnmatch(fname,pat): 
       fullname = os.path.join(path,fname) 
       with open(fullname) as f: 
        # This expects the copyright to be in the first 1000 bytes 
        # of the data to speed up the search. 
        if userstring.search(f.read(1000)): 
         print fullname 

这里有一个文件,上面的代码将匹配:

blah 
blah 
Copyright (c) 2012 Company, Inc 
All Rights Reserved. 
Company Confidential and Proprietary. 
blah 
blah 
blah 
+0

报价其实我是想打印的犯规匹配的模式 – user1927233

+0

文件'如果不是userstring.search' ... –

0

对于问题1,你可能需要使用os.path.splitext()

>>> os.path.splitext('/home/myfile.txt') 
('/home/myfile', '.txt') 
+0

正在使用基于来自史蒂芬下面的建议,但运行到编译错误的endsWith的所有支持,所以不知道什么是错在这里 – user1927233

0

为了解决第一个问题:

如果你想找到一个具有某种扩展名可以随时结尾的文件在包含文件名的str上使用endswith()方法。例如这样的事情:

if filename.endswith("." + extension1) or filename.endswith("." + extension2) 

文件名会像“foo.c的”扩展1和一个STR会像“C”和扩展名2另一STR将是“CPP”。

源:http://docs.python.org/2/library/stdtypes.html#str.endswith

+0

我根据您的建议更新的代码,运行到编译错误,哪里不对? – user1927233

+0

http://snipt.org/zaYg4我修正了它。 –

+0

问题是,C和CPP是变量不是字符串文字 –

相关问题