I'm completely new to python and I'm trying to rename a set of files using a string on a specific line within the file and use it to rename the file. Such string is found at the same line in each file.
As an example:
- 10 files in the same path
- the string is found in line number 14 and it starts at character number 40 and it is 50 characters in length
- Then use the extracted string to rename the respective file
I'm trying to use this code, but I fail to figure out how to get it working:
for filename in os.listdir(path):
if filename.startswith("out"):
with open(filename) as openfile:
fourteenline = linecache.getline(path, 14)
os.rename(filename, fourteenline.strip())