# -*- coding: utf-8 -*-
#!/usr/bin/python
import os
def listFiles():
fileDir = r"C:\Users\syj\Desktop\111\test1\xml"
fileList = []
for root, dirs, files in os.walk(fileDir):
for fileObj in files:
fileList.append(os.path.join(root, fileObj))
for fileObj in fileList:
f = open(fileObj,'r+')
all_the_lines = f.readlines()
f.seek(0)
f.truncate()
for line in all_the_lines:
#修改头文件
f.write(line.replace('<?xml version="1.0" ?>', '<?xml version="1.0" encoding="utf-8" ?>'))
#删除头文件
#f.write(line.replace('<?xml version="1.0" ?>', ''))
#print(line)
f.close()
if __name__ =='__main__':
listFiles()
python批量去掉xml文件的头部或修改头部、解决编码错误问题
于 2022-04-13 15:41:10 首次发布