Im looping through a file with multiple log files that have been assigned a randomized filename. The script should search within the log for the line containing "hostname" and then pull out the assigned hostname to use as the re-assigned file name, which is where im having trouble...total noob....
import os
path = "/Users/Joel/Desktop/logs"
for file in os.listdir(path):
with open(f"{path}/{file}") as log:
for line in log:
if "hostname" in line:
desired_filename = line.split()[1].replace('"','')
os.rename(f'{path}/{file}', f'{path}/{desired_filename}')
Using
fileas a variable name in the second line replaces the same variable from the first line. You probably want to use different names for these two variables.You probably need to add
pathto the front of both of these filenames.