PROGRAMMING

m9/ PYTHON
REMEMBERS




Pipes

p255 ! Programs launched from the shell can also be launch from Python using pipe object. The return value is an object that behaves like a file.
 
import os

cmd = 'ls -l'
fp = os.popen(cmd)
res = fp.read()
print(res)
    # -rw-rw-r-- 1 catalin catalin 16384 iul 15 12:35 images_db
    # drwxr-xr-x 3 catalin catalin  4096 iul 13 09:35 layouts
    # drwxr-xr-x 5 catalin catalin  4096 iun  4  2020 library

stat = fp.close()
print(stat) # None (no errors)
... 4 lines
 
Using pipe you can md5sum to check if two files have the same content.
 
import os

def md5sum (filename):
    cmd = 'md5sum ' + filename
    fp = os.popen(cmd)
    res = fp.read() # 5a92d5b6e2303c7fea60297d2c985713  images_db
    md5sum = res.replace(filename, '')
    stat = fp.close()
    return md5sum

assert md5sum('images_db') == md5sum('images_db_copy') # pass

... 3 lines
 

Questions    
Definition

        A B C D E F
🔔
1/1