Skip to content

Commit 4166f0b

Browse files
Better print_file_lines
1 parent 3023927 commit 4166f0b

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/smolagents/bp_tools.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,25 @@ def print_file_lines(filename: str, start_line: int, end_line: int) -> None:
311311
"""
312312
This tool prints the lines from the start_line to the end_line of the file filename.
313313
In combination with get_line_from_file, this tool is useful for finding bugs in the source code.
314-
314+
The first line of the file is the line number 1.
315+
print_file_lines('filename.txt', -2, 0) will print the last 3 lines of the file.
315316
Args:
316317
filename: str The path to the text file.
317318
start_line: int
318319
end_line: int
319320
"""
320321
file_content = load_string_from_file(filename)
321322
lines = file_content.splitlines()
323+
last_line = len(lines)
324+
if end_line > last_line:
325+
end_line = last_line
326+
if start_line > end_line:
327+
start_line = end_line
322328
print("Content of " + filename + " from line "+str(start_line)+" to line " +str(end_line) )
323329
for i in range(start_line-1, end_line):
330+
if i<0:
331+
print(f"{i+1+last_line}: {lines[i]}")
332+
else:
324333
print(f"{i+1}: {lines[i]}")
325334

326335
@tool

0 commit comments

Comments
 (0)