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))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With