Background: I am trying to automate some parts of a work process. Manual steps I take: I get a ticket to change an ID inside a text file. I have to go through a network shared folder, create a backup of the file, push the copied file into an archive folder, and edit the existing file with the ID from the ticket.
What I am trying to create: A small program that will ask me what ID I am wanting to change(there are pre-existing ID's inside the text file) then it will go inside the file and find the match. Then I want the program to ask me what I want to change the ID to. I want the program to edit the existing file with my input, save it, then close it.
So far I have the following code that completes the first portion (copying the file and pushing it into the archive folder). I have the second function which I am stuck on. I know the second function isn't working, but would like input from others on what they think I should try. I have seen the fileinput module, but I have read that it's not the best module and I should try to program this without the fileinput module.
Code:
import shutil
import datetime
import os
import fileinput
original_file = "\\network\\path\\to\\file"
def date_rename_file():
todays_date = datetime.datetime.now()
stripped_time = todays_date.strftime('%Y%m%d')
shutil.copyfile(original_file, "\\network\\path\\to\\backupfolder\\device_"+str(stripped_time)+".txt")
def device_id_replace():
original_id = input("What device ID are you needing to replace?")
new_id = input("What is the new device ID?")
with open(original_file, 'w') as devicetxt:
for line in devicetxt:
print(line)
if original_id in line:
print("Found "+original_id)
date_rename_file()
device_id_replace()
Thanks, feel free to demolish my code :) I'm still learning and would appreciate any input. Also, feel free to let me know if I left any pertinent information out!