2017-02-01 82 views
1

我得到项目中的文件列表,其范围可以从src/app.tssrc/component/app/app.ts。什么我希望做的是:Python:如果列表中不存在文件,则创建文件

  • 遍历列表中的每个文件,
  • 看它是否特定配置的模式相匹配,
  • ,如果文件不存在,把它写到磁盘。

目前我有:

m = re.compile(r'(ts|js)config.json$') 
for file in files: 
    if m.search(file): 
     return True 
    else: 
     self.writeFile() 

其中一期工程,但它要求写多次时,有无法比拟的。 检查完成后,我将如何才能调用写入?

回答

1

你可以只取消缩进你else块所以它适用于for

for file in files: 
    if m.search(file): 
     return True 
else: 
    self.writeFile() 

注意,在这种情况下,它不是与break情况下有趣的,你可以简单地写:

for file in files: 
    if m.search(file): 
     return True 
self.writeFile() 

,因为如果模式匹配,则返回,因此writeFile未到达。

1

你可以搬出该文件写入后,所有检查都用尽:

m = re.compile(r'(ts|js)config.json$') 
for file in files: 
    if m.search(file): 
     return True 

self.writeFile() 
相关问题