search

Home  >  Q&A  >  body text

python - 对文件夹内文件处理

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

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

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

高洛峰高洛峰2787 days ago777

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 10:30:13

    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

    reply
    0
  • Cancelreply