如何将文件搜索限制为仅限特定扩展名

时间:2012-12-25 00:23:10

标签: python python-2.7 file-extension

我有一个基本问题(下面的#1)和一个我不知道(#2)关于答案的问题。任何人都可以提供投入吗?

1.如何将搜索限制为仅限extensions,仅限.c.h.cpp

2.如何在可选

下面的"."变量"\n"之前制作点usertring
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

编译错误: -

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

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

解决第一个问题:

如果要查找以特定扩展名结尾的文件,可以始终在包含文件名的str上使用endswith()方法。例如:

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

filename将是一个像“foo.c”的str,而extension1将是另一个像“c”的str,而extension2将是“cpp”。

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

答案 2 :(得分: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