TGViewer
About Python [ru] About Python [ru] @python_tesst · 6.44K subscribers
Post #2540 813
👩‍💻 Задачка по Python

Напишите скрипт, который принимает два текстовых файла и выводит строки, которые отличаются между ними.

➡️ Пример:

python compare_files.py file1.txt file2.txt

Различия:
file1.txt: Python is amazing.
file2.txt: Python is great.
file1.txt: This is file one.
file2.txt: This is file two.


Решение задачи ⬇️

def compare_files(file1_path, file2_path):
try:
with open(file1_path, 'r', encoding='utf-8') as file1, open(file2_path, 'r', encoding='utf-8') as file2:
file1_lines = file1.readlines()
file2_lines = file2.readlines()

max_lines = max(len(file1_lines), len(file2_lines))
print("Различия:")

for i in range(max_lines):
line1 = file1_lines[i].strip() if i < len(file1_lines) else "<пусто>"
line2 = file2_lines[i].strip() if i < len(file2_lines) else "<пусто>"

if line1 != line2:
print(f"file1.txt: {line1}")
print(f"file2.txt: {line2}")

except FileNotFoundError as e:
print(f"Ошибка: {e}")
except Exception as e:
print(f"Ошибка: {e}")

# Пример использования:
if __name__ == "__main__":
import sys
if len(sys.argv) < 3:
print("Использование: python compare_files.py <file1> <file2>")
else:
compare_files(sys.argv[1], sys.argv[2])
More from @python_tesst
  1. Sep 30, 2026Google закинули AI-агента в 20+ чатов живых команд. На 5 месяцев. Вышло забавно. Агенту да…
  2. Sep 30, 2026Post #3199
  3. Sep 29, 2026Claude Sonnet 5.5 — уже не слив, релиз официально состоялся Прайс не трогали: $2 за миллио…
  4. Sep 29, 2026Post #3194
  5. Sep 29, 2026Дженсен Хуанг выкатил NVIDIA Open Agent Safety Platform — и притащил с собой 100+ партнеро…
  6. Sep 29, 2026Стикмену теперь под силу снести любой сайт. Чувак под это навайбкодил целую игруху: грузиш…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →