UbuntuConfig/user_updater.py

52 lines
1.9 KiB
Python
Raw Permalink Normal View History

2024-09-20 16:16:58 +00:00
import os
import getpass
import shutil
2024-09-20 16:16:58 +00:00
def replace_word_in_files(target_word, replacement_word, folder_path=".", dest_path=".."):
2024-09-20 16:16:58 +00:00
# Loop through all files in the specified folder
for filename in os.listdir(folder_path):
if filename.endswith(".desktop"): # Only process text files
file_path = os.path.join(folder_path, filename)
with open(file_path, 'r') as file:
file_content = file.read()
# Replace the target word with the replacement word
new_content = file_content.replace(target_word, replacement_word)
# Write the new content back to the file
file_path = os.path.join(dest_path, filename)
2024-09-22 01:31:09 +00:00
if os.path.exists(file_path):
2024-09-22 01:32:25 +00:00
os.remove(file_path)
2024-09-22 01:31:09 +00:00
2024-09-20 16:16:58 +00:00
with open(file_path, 'w') as file:
file.write(new_content)
print(f"Replaced '{target_word}' with '{replacement_word}' in {filename}")
def copy_png_files_up():
current_dir = os.getcwd() # Get the current working directory
parent_dir = os.path.abspath(os.path.join(current_dir, '..')) # Get the parent directory
# Iterate through all files in the current directory
for filename in os.listdir(current_dir):
if filename.endswith(".png"): # Check if the file is a .png file
source_path = os.path.join(current_dir, filename)
destination_path = os.path.join(parent_dir, filename)
2024-09-22 01:31:09 +00:00
if os.path.exists(destination_path):
os.remove(destination_path)
# Copy the .png file to the parent directory
shutil.copy(source_path, destination_path)
print(f"Copied: {filename} to {parent_dir}")
2024-09-20 16:16:58 +00:00
if __name__ == "__main__":
# Get the current Ubuntu username
username = getpass.getuser()
# Replace 'TARGET_WORD' in all text files with the username
target_word = "userhere"
replace_word_in_files(target_word, username)
copy_png_files_up()
2024-09-20 16:16:58 +00:00