You can perform various operations on text files like data_file.txt and test_file.txt. Here are some common operations:
-
Reading Files: Load the contents of the files into your program.
with open('data_file.txt', 'r') as file: data = file.read() -
Writing to Files: Save data to the files.
with open('test_file.txt', 'w') as file: file.write("Some new content") -
Appending Data: Add data to the end of a file.
with open('data_file.txt', 'a') as file: file.write("\nAdditional content") -
Comparing Files: Check if the contents of the files are the same.
with open('data_file.txt', 'r') as file1, open('test_file.txt', 'r') as file2: are_equal = file1.read() == file2.read() -
Searching for Content: Look for specific strings or patterns in the files.
with open('data_file.txt', 'r') as file: if "search_term" in file.read(): print("Found!") -
Sorting Lines: Read lines from a file and sort them.
with open('data_file.txt', 'r') as file: sorted_lines = sorted(file.readlines()) -
Data Processing: Process the data (e.g., parsing, filtering).
with open('data_file.txt', 'r') as file: processed_data = [line.strip() for line in file if line.strip()]
Feel free to ask if you need examples for a specific operation!
