mirror of
https://github.com/hubHarmony/servii-backend.git
synced 2024-11-18 05:50:31 +00:00
37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
|
import re
|
||
|
|
||
|
|
||
|
def update_server_property(file_path, property_name, new_value):
|
||
|
"""
|
||
|
Updates a specified property in a Minecraft server configuration file.
|
||
|
|
||
|
Parameters:
|
||
|
- file_path: Path to the Minecraft server configuration file.
|
||
|
- property_name: Name of the property to update.
|
||
|
- new_value: New value to set for the property.
|
||
|
"""
|
||
|
# Regular expression pattern to match lines containing the property name
|
||
|
pattern = rf'^{property_name}=.*$'
|
||
|
|
||
|
# Read the file content
|
||
|
with open(file_path, 'r') as file:
|
||
|
content = file.readlines()
|
||
|
|
||
|
# Find the line containing the property and update its value
|
||
|
for i, line in enumerate(content):
|
||
|
if re.match(pattern, line):
|
||
|
content[i] = f"{property_name}={new_value}\n"
|
||
|
break
|
||
|
else:
|
||
|
raise ValueError(f"Property '{property_name}' not found in the file.")
|
||
|
|
||
|
# Write the updated content back to the file
|
||
|
with open(file_path, 'w') as file:
|
||
|
file.writelines(content)
|
||
|
|
||
|
# Example usage
|
||
|
file_path = 'folder/server.properties'
|
||
|
property_name = 'server-ip'
|
||
|
new_value = '127.0.0.1'
|
||
|
update_server_property(file_path, property_name, new_value)
|