使用下面的代码,我制作了一个htmlfiles.txt,该文件在目录中包含HTML文件名:
import os
entries = os.listdir('/home/stupidroot/Documents/html.files.test')
count=0
for line in entries:
count += 1
f = open("htmlfiles.txt", "a")
f.write(line + "\n")
f.close()
在第二阶段,我想对每个文件进行如下修改:
lines = open('filename.html').readlines()
open('filename.html', 'w').writelines(lines[20:-20])
此代码删除HTML文件中的前20行。
我只想使所有带有for循环的文件变得简单。
您只需要打开htmlfiles.txt
文件,并从每一行读取每个文件名,然后执行以下操作:
with open("htmlfiles.txt") as fic:
for filename in fic:
filename = filename.rstrip()
lines = open(filename).readlines()
open(filename, 'w').writelines(lines[20:-20])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句