Bài tập File Operations - Cơ bản
- Viết chương trình tạo file "hello.txt" và ghi dòng chữ "Hello World".
# Code của bạn ở đây- Viết hàm
read_fileđọc và in toàn bộ nội dung file.
def read_file(filename):
# Code của bạn ở đây
pass
# Test
read_file("data.txt")- Viết hàm
count_linesđếm số dòng trong file.
def count_lines(filename):
# Code của bạn ở đây
pass
# Test
print(count_lines("data.txt"))- Viết hàm
append_textthêm một dòng text vào cuối file.
def append_text(filename, text):
# Code của bạn ở đây
pass
# Test
append_text("log.txt", "New log entry")- Viết hàm
read_first_lineđọc dòng đầu tiên của file.
def read_first_line(filename):
# Code của bạn ở đây
pass
# Test
print(read_first_line("data.txt"))- Viết hàm
write_list_to_fileghi list các chuỗi vào file, mỗi chuỗi một dòng.
def write_list_to_file(filename, items):
# Code của bạn ở đây
pass
# Test
fruits = ["apple", "banana", "orange"]
write_list_to_file("fruits.txt", fruits)- Viết hàm
file_existskiểm tra file có tồn tại không.
def file_exists(filename):
# Code của bạn ở đây
pass
# Test
print(file_exists("data.txt")) # True hoặc False- Viết hàm
copy_filecopy nội dung từ file này sang file khác.
def copy_file(source, destination):
# Code của bạn ở đây
pass
# Test
copy_file("input.txt", "output.txt")- Viết hàm
count_wordsđếm tổng số từ trong file.
def count_words(filename):
# Code của bạn ở đây
pass
# Test
print(count_words("data.txt"))- Viết hàm
read_lines_to_listđọc file và trả về list các dòng.
def read_lines_to_list(filename):
# Code của bạn ở đây
pass
# Test
lines = read_lines_to_list("data.txt")
print(lines)- Viết hàm
write_numbersghi các số từ 1 đến n vào file, mỗi số một dòng.
def write_numbers(filename, n):
# Code của bạn ở đây
pass
# Test
write_numbers("numbers.txt", 10)- Viết hàm
search_in_filetìm kiếm một từ trong file và trả về số dòng chứa từ đó.
def search_in_file(filename, search_word):
# Code của bạn ở đây
pass
# Test
search_in_file("data.txt", "Python")- Viết hàm
get_file_sizetrả về kích thước file (bytes).
def get_file_size(filename):
# Code của bạn ở đây
pass
# Test
print(f"File size: {get_file_size('data.txt')} bytes")- Viết hàm
replace_in_filethay thế tất cả từ old_word bằng new_word trong file.
def replace_in_file(filename, old_word, new_word):
# Code của bạn ở đây
pass
# Test
replace_in_file("data.txt", "old", "new")- Viết hàm
read_file_safeđọc file với exception handling.
def read_file_safe(filename):
try:
# Code của bạn ở đây
pass
except FileNotFoundError:
return "File không tồn tại"
except Exception as e:
return f"Lỗi: {e}"
# Test
content = read_file_safe("data.txt")- Viết hàm
write_dict_to_fileghi dictionary vào file dạng key=value.
def write_dict_to_file(filename, data):
# Code của bạn ở đây
pass
# Test
config = {"name": "Alice", "age": "25", "city": "Hanoi"}
write_dict_to_file("config.txt", config)- Viết hàm
read_dict_from_fileđọc file dạng key=value thành dictionary.
def read_dict_from_file(filename):
# Code của bạn ở đây
pass
# Test
config = read_dict_from_file("config.txt")
print(config)- Viết hàm
remove_empty_linesxoá các dòng trống trong file.
def remove_empty_lines(filename):
# Code của bạn ở đây
pass
# Test
remove_empty_lines("data.txt")- Viết hàm
merge_filesgộp nội dung nhiều file vào một file.
def merge_files(output_file, *input_files):
# Code của bạn ở đây
pass
# Test
merge_files("combined.txt", "file1.txt", "file2.txt", "file3.txt")- Viết chương trình To-Do List đơn giản lưu vào file "todo.txt".
def add_task(task):
# Code của bạn ở đây
pass
def show_tasks():
# Code của bạn ở đây
pass
def clear_tasks():
# Code của bạn ở đây
pass
# Test
add_task("Học Python")
add_task("Làm bài tập")
show_tasks()