-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_utils.py
More file actions
36 lines (26 loc) · 912 Bytes
/
file_utils.py
File metadata and controls
36 lines (26 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
def ensure_directory_exists(directory):
if directory and not os.path.exists(directory):
os.makedirs(directory, exist_ok=True)
def get_file_contents(path):
try:
with open(path, 'r') as f:
return f.read()
except Exception as e:
print(f"Error reading file {path}: {e}")
return ""
def update_file(filepath, content):
directory = os.path.dirname(filepath)
ensure_directory_exists(directory)
with open(filepath, 'w') as f:
f.write(content)
def move_file(old_path, new_path):
directory = os.path.dirname(new_path)
ensure_directory_exists(directory)
if os.path.exists(old_path):
os.rename(old_path, new_path)
def find_file_in_project(filename, project_dir="."):
for root, dirs, files in os.walk(project_dir):
if filename in files:
return os.path.join(root, filename)
return ""