通过输入指定文件夹的路径,即可完成该功能,代码运行无误。
import os
def updateFile(file):
with open(file, "w", encoding='utf-8') as f:
f.seek(0)#将文件指针移动到文件开头
f.truncate()#清空文件内容
f.write('The plane blade area has crack defects.')
f.close()
print("文件操作成功")
def file_list(path):
file_name = os.listdir(path)
for file in file_name:
if os.path.splitext(file)[1] == '.txt':
file_path = os.path.join(path, file)
updateFile(file_path)
path = '/media/ai/Teamwork/kohya_ss/images/labels_txt'
file_list(path)