@@ -122,13 +122,13 @@ def __view(self, path, view_range):
122122 return f"Error: Path { path } does not exist"
123123
124124 if abs_path .is_file ():
125- with open (abs_path , "r" ) as f :
125+ with open (abs_path , "r" , newline = '' ) as f :
126126 content = f .read ()
127127
128128 if view_range :
129- lines = content .splitlines ()
129+ lines = content .splitlines (keepends = True )
130130 start , end = view_range
131- content = "\n " .join (lines [start - 1 : end ])
131+ content = "" .join (lines [start - 1 : end ])
132132 return content
133133 elif abs_path .is_dir ():
134134 result = []
@@ -146,7 +146,8 @@ def __create(self, file_text, path):
146146 if abs_path .exists ():
147147 return f"Error: File { path } already exists"
148148 abs_path .parent .mkdir (parents = True , exist_ok = True )
149- abs_path .write_text (file_text )
149+ with open (abs_path , 'w' , newline = '' ) as f :
150+ f .write (file_text )
150151 return f"File created successfully at: { path } "
151152
152153 def __str_replace (self , new_str , old_str , path ):
@@ -155,12 +156,13 @@ def __str_replace(self, new_str, old_str, path):
155156 return f"Error: File { path } does not exist"
156157 if not abs_path .is_file ():
157158 return f"Error: File { path } is not a file"
158- content = abs_path .read_text ()
159+ with open (abs_path , 'r' , newline = '' ) as f :
160+ content = f .read ()
159161 occurrences = content .count (old_str )
160162 if occurrences != 1 :
161163 return f"Error: Found { occurrences } occurrences of old_str, expected exactly 1"
162164 new_content = content .replace (old_str , new_str )
163- with open (abs_path , "w" ) as f :
165+ with open (abs_path , "w" , newline = '' ) as f :
164166 f .write (new_content )
165167 return "Replacement successful"
166168
@@ -169,10 +171,12 @@ def __insert(self, insert_line, new_str, path):
169171 if not abs_path .is_file ():
170172 return f"Error: File { path } does not exist or is not a file"
171173
172- lines = abs_path .read_text ().splitlines (keepends = True )
174+ with open (abs_path , 'r' , newline = '' ) as f :
175+ lines = f .readlines ()
173176 if insert_line is None or insert_line < 1 or insert_line > len (lines ):
174177 return f"Error: Invalid insert line { insert_line } "
175178
176179 lines .insert (insert_line , new_str + "\n " )
177- abs_path .write_text ("" .join (lines ))
180+ with open (abs_path , 'w' , newline = '' ) as f :
181+ f .writelines (lines )
178182 return "Insert successful"
0 commit comments