-1

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

1.如何将搜索限制为仅特定的,extensions让我们说只有.c,,,.h.cpp

2.如何使下面的in变量"."之前的点成为可选的"\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
4

3 回答 3

0

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

>>> os.path.splitext('/home/myfile.txt')
('/home/myfile', '.txt')
于 2012-12-25T00:30:14.140 回答
0

解决第一个问题:

如果您想查找以某个扩展名结尾的文件,您始终可以在包含文件名的 str 上使用 endswith() 方法。例如这样的:

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

文件名将是类似“foo.c”的str,而extension1 将是另一个类似“c”的str,而extension2 将是“cpp”。

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

于 2012-12-25T00:37:20.597 回答
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
于 2012-12-25T01:07:39.927 回答