Python
/
Storage
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Storage Pipes
Shell commands, launched from Python Pipe object, any running program import os fp = os.popen('ls -l')
Pipes
p255 Programs launched from the shell can also be launch from Python.
# Pipe object:
#
# 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)
Check
Using pipe you can md5sum to check if two files have the same content.
# Using pipe ...
# you can use md5sum ...
# to check if two files have the same content.
import os
DIR = os.path.dirname(os.path.realpath(__file__))
file1 = DIR + "/data/01.txt"
f = open(file1, "w")
f.write("1234567890")
f.close()
file2 = DIR + "/data/02.txt"
f = open(file2, "w")
f.write("1234567890")
f.close()
def md5sum (filename):
cmd = 'md5sum ' + filename
fp = os.popen(cmd)
res = fp.read()
print(res)
# 5a92d5b6e2303c7fea60297d2c985713 01.txt
md5sum = res.replace(filename, '')
stat = fp.close()
return md5sum
assert md5sum(file1) == md5sum(file2) # pass
➥ Questions