-3

基本上,我只想列出那些 windows 不会创建的文件。我如何列出 4.txt 和 4a.txt ...... s == s是真的吗?

myList = '''
4.txt
4a.txt
spam
'''
myregex = 'a' 
s = re.sub(myregex,'',myList)

if s == s:    
    print "got it!"  # prints 'got it'
    print s == s     # prints 'true'
4

2 回答 2

0

你不需要正则表达式。
只需检查文件是否存在os.path.exists

import os
lst = [your_fileNames....]

for arch in lst:
    if not os.path.exists(arch):
        print arch
于 2013-09-11T05:49:44.907 回答
0

代码:

import re
myList = '''
4.txt
4a.txt
spam
'''.split()
myList
reList = [
    '4\w?\.txt', #4 followed by 0 or 1 alphanumeric then .txt
    '4.*\.txt',   #4 followed by anything and then .txt
    '\d\w?\.txt', #a digit, then 0 or 1 alphanumeric then .txt
    ]

for name in myList:
    for regex in reList:
        if re.match(regex, name):
            print ("%s matches %s" % (name, regex))
        else:
            print ("%s does not match %s" % (name, regex))

输出:

=================================重新开始================= ================

4.txt matches 4\w?\.txt
4.txt matches 4.*\.txt
4.txt matches \d\w?\.txt
4a.txt matches 4\w?\.txt
4a.txt matches 4.*\.txt
4a.txt matches \d\w?\.txt
spam does not match 4\w?\.txt
spam does not match 4.*\.txt
spam does not match \d\w?\.txt
于 2013-09-11T01:36:31.903 回答