thienphuc12339's picture
Upload 12 files
dddf3f6 verified
import os
def create_text_file(filename, content, directory=""):
"""Create a text file with the given content."""
if directory and not os.path.exists(directory):
os.makedirs(directory)
with open(os.path.join(directory, filename), 'w') as file:
file.write(content)
def save_text_file(filename, content, directory=""):
"""Save content to a text file."""
create_text_file(filename, content, directory)
def read_all_text_files_in_folder(directory):
"""Read and return the content of all text files in the specified folder."""
contents = []
for filename in os.listdir(directory):
if filename.endswith(".txt"):
with open(os.path.join(directory, filename), 'r') as file:
contents.append(file.read())
return contents
def append_to_text_file(filename, content, directory=""):
"""Append content to an existing text file."""
with open(os.path.join(directory, filename), 'a') as file:
file.write(content)