文件夹目录如下(需要递归删除文件夹下的.DS_Store文件):
import os
import os. pathpath = "name/labels"
files = os. listdir( path)
s = [ ] for xmlFile in files: xmlpath = os. path. join( path, xmlFile) if os. path. splitext( xmlpath) [ 1 ] == ".xml" : try : f_read = open ( xmlpath, "r" , encoding= 'utf-8' ) for line in f_read. readlines( ) : if 'www' in line: print ( xmlpath) except : print ( 'no xml' )
print ( "search over!" )
查找指定路径的子文件夹下所有xml文件中带有指定字符的xml文件(这个需要删除文件夹下的.DS_Store文件)
import os
import os. pathpath = "name"
files = os. listdir( path)
s = [ ] for subFile in files: file_path = os. path. join( path, subFile) xmlfiles = os. listdir( file_path) for xmlFile in xmlfiles: xmlpath = os. path. join( file_path, xmlFile) print ( xmlpath) if os. path. splitext( xmlpath) [ 1 ] == ".xml" : try : f_read = open ( xmlpath, "r" , encoding= 'utf-8' ) for line in f_read. readlines( ) : if 'www' in line: print ( xmlpath) except : print ( 'no xml' )
import osfolder_path = "/Users/liyongbo/Desktop/test" for root, dirs, files in os. walk( folder_path) : for filename in files: if filename == '.DS_Store' : file_path = os. path. join( root, filename) os. remove( file_path) print ( "delete ok" )