Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming files will not work with >1 GB file

I'm using the below code to rename few hundreds of files, it works great when the file is smaller than 1GB in size, but when it encounter larger files it doesn't extract anything and the resulted file name is blank.

import os, linecache

for filename in os.listdir(path):
    if not filename.startswith("out"): continue # less deep
    file_path = os.path.join(path, filename) # folderpath + filename
    fourteenline = linecache.getline(file_path, 14) # maybe 13 for 0-based index?
    new_file_name = fourteenline[40:40+50].rstrip() # staring at 40 with length of 50
    os.rename(file_path, os.path.join(path, new_file_name))
like image 450
Moh Avatar asked Dec 04 '25 13:12

Moh


1 Answers

Don't use linecache. It reads the entire files in memory and fails quietly when running out of memory.

Just open each file and read 14 lines in a simple loop, or with help of itertools.islice.

like image 152
Janne Karila Avatar answered Dec 07 '25 15:12

Janne Karila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!