Posts

Find difference between two text files in Python

Image
#!/usr/bin/python import difflib, sys file1 = sys.argv[1] file2 = sys.argv[2] diff = difflib.unified_diff ( open(file1).readlines(), open(file2).readlines(), fromfile=file1, tofile=file2 ) strings = list() for line in diff:     strings.append(line.replace('\n', '')) for string in strings:     print(string) This script compares two text files in Python using difflib and prints a unified diff, the same format version control tools use. You pass the two file paths as arguments. difflib ships with the standard library, so nothing needs installing, and it is handy for comparing generated output, logs, or configuration against an expected version.

Find difference between two text files in Perl

Image
#!/usr/bin/env perl use strict; use warnings; use Text::Diff; my $file1 = $ARGV[0]; my $file2 = $ARGV[1]; my $diffs = diff $file1,$file2; my @strings = split("\n",$diffs); foreach(@strings) {     print $_."\n"; } This Perl script compares two text files and prints their differences using the Text::Diff module. You pass the two file paths as arguments and it reports the changed lines, much like the Unix diff command. Doing this in code is useful inside larger pipelines, for example checking generated output against an expected baseline or flagging configuration drift between environments.

Convert String to Base64 in Python

Image
Code to convert string (from utf-8) to base64 encoding and base64 back to utf-8: import base64 si = input('\nenter text:\n') print('\nyou entered\n' + si) b64 = base64.b64encode(bytes(si, 'utf-8')) print('\nbase64 is\n' + str(b64)) b64_bs = bytearray(base64.b64encode(bytes(si, 'utf-8'))) print('\nbase64 byte stream is\n' + str(list(b64_bs))) so = base64.b64decode(b64).decode('utf-8', 'ignore') print('\nstring is\n' + so) This example encodes a string to Base64 and decodes it back in Python using the base64 module. Base64 represents arbitrary bytes as plain text, which is useful when binary data must travel through text only channels such as JSON or email. Remember that Base64 is an encoding, not encryption: it provides no secrecy and grows the data by about a third. Encode for transport, not for protection.

Convert String to Binary in Python

Image
Code to convert string to binary and binary to string: def string2bits(s=''):     return [bin(ord(x))[2:].zfill(8) for x in s] def bits2string(b=None):     return ''.join([chr(int(x, 2)) for x in b]) si = input('\nenter text:\n') b = string2bits(si) so = bits2string(b) print('\nyou entered\n' + si) print('\nbinary is\n' + str(b)) print('\nstring is\n' + so) This example converts text to its binary representation and back in Python by mapping each character to its 8 bit code with ord and bin, then reversing the process with int and chr. It is a useful exercise for understanding character encoding and how text is stored as bytes underneath. For real binary handling, Python's bytes type and struct module are the practical tools.

TypeError: Can't convert 'bytes' object to str implicitly

Image
Error: Traceback (most recent call last):   File "b64-str.py", line 10, in <module>     print('\nstring is\n' + so) TypeError: Can't convert 'bytes' object to str implicitly Problem Code: so = base64.b64decode(b64) print('\nstring is\n' + so) Solution: so = base64.b64decode(b64).decode('utf-8', 'ignore') print('\nstring is\n' + so) This Python 3 error happens when you join bytes and str with the plus operator. Python 3 keeps bytes and text strictly separate, so they cannot be concatenated directly. Calling .decode() on the bytes, or .encode() on the string, converts one to the other so the types match. This is one of the most common surprises when moving code from Python 2 to Python 3.

AttributeError: module 'json' has no attribute 'dump'

Image
Problem: Today I was trying to create a json file from Python and then I got this error: "AttributeError: module 'json' has no attribute 'dump'" and I found my solution here: http://stackoverflow.com/a/13612382/4064166 Solution: So the problem was I named my file as json.py that was causing problems with json module so renaming it solved the issue. Code: import json with open("dump.json", "w") as outfile:     json.dump({'number':6650, 'strings':'lorem ipsum', 'x':'x', 'y':'y'}, outfile, sort_keys = True, indent=4, ensure_ascii=False) Screenshot:

Make file read-only in Python

Image
If you want to make file read-only: import os from stat import S_IREAD os.chmod('path/to/file.txt', S_IREAD) If you want to make file writeable: import os from stat import S_IWRITE os.chmod('path/to/file.txt', S_IWRITE) Ref. 1: http://stackoverflow.com/a/28492823/4064166 Ref. 2: http://techarttiki.blogspot.in/2008/08/read-only-windows-files-with-python.html This snippet toggles a file between read-only and writable in Python using os.chmod with stat flags. Setting S_IREAD makes it read-only; S_IWRITE makes it writable again. It is handy for protecting generated output or configuration from accidental edits. On Unix like systems the effect also depends on ownership and the existing permissions.