python - 对文件夹内文件处理
高洛峰
高洛峰 2017-04-18 10:28:13
0
1
760

对文件夹内的htm文件进行提取(卡在正则,因为文件夹内还有许多其他类型文件)

再对所有文件内容进行添加(每一个文件,从304到717)

添加后对文件进行重命名保存

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
巴扎黑

In fact, if you simply search for files, you don’t need regular expressions. If you use the files in the folder, you can use the glob module to get a list of file names, such as

import glob
import shutil
file_list = glob.glob('*.htm')  # ['1.htm', '2.htm', '3.htm']

After getting the list, you can traverse the list and perform the processing you want

for i in file_list:
    old_fileName = i
    new_fileName = i + '.tmp'
    #另存为:
    shutil.copy(old_fileName, new_fileName)
    with open(new_fileName, 'r+') as f:
       #光标移动到末尾
       f.seek(0,2)
       f.write('\nwrite something')
       #f.flush()

Can additionally process and save files

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!