当Windows无法创建已存在的文件时,正则表达式列表文件

时间:2013-09-11 00:49:42

标签: python regex

基本上,我只想列出那些窗口不会创建的文件。 如何列出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'

2 个答案:

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

输出:

  
    
      

================================ RESTART ============== ==================

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
    
  

答案 1 :(得分:0)

你不需要正则表达式。
只需使用os.path.exists

检查文件是否存在
import os
lst = [your_fileNames....]

for arch in lst:
    if not os.path.exists(arch):
        print arch