Renaming files does not work with files >1 GB… here is a solution to the problem.
Renaming files does not work with files >1 GB
I’m renaming hundreds of files using the code below and it works fine when the file is smaller than 1GB, but it doesn’t extract anything when it encounters a larger file and the resulting filename 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))
Solution
Don’t use linecache
. It reads the entire file in memory and fails quietly when it runs out of memory.
Just open
each file and read 14 lines in a simple loop, or with the help of itertools.islice.