ساخت مسیر امن با pathlib #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from pathlib import Path
p=Path.home()/'Downloads'/'file.txt'
print(p)
PythonHost.ir | پایتون هاست
@pythonhost_ir
👍1
خواندن فایل امن با encoding #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from pathlib import Path
text=Path('a.txt').read_text(encoding='utf-8')
print(text[:200])
PythonHost.ir | پایتون هاست
@pythonhost_ir
👍1
نوشتن فایل با pathlib #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from pathlib import Path
Path('out.txt').write_text('سلام', encoding='utf-8')
print('ok')
PythonHost.ir | پایتون هاست
@pythonhost_ir
👍2
وجود داشتن فایل/پوشه با pathlib #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from pathlib import Path
p=Path('out.txt')
print(p.exists(), p.is_file())
PythonHost.ir | پایتون هاست
@pythonhost_ir
❤1👍1👎1
لیست کردن فایلها با glob #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import glob
print(glob.glob('*.py')[:10])
PythonHost.ir | پایتون هاست
@pythonhost_ir
فیلتر نام فایلها با fnmatch #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import fnmatch
names=['a.py','b.txt','c.py']
print([n for n in names if fnmatch.fnmatch(n,'*.py')])
PythonHost.ir | پایتون هاست
@pythonhost_ir
❤1
ساخت پوشه اگر نبود #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from pathlib import Path
Path('data').mkdir(exist_ok=True)
print('ready')
PythonHost.ir | پایتون هاست
@pythonhost_ir
👍1
حذف پوشه کامل با shutil #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import shutil
shutil.rmtree('data', ignore_errors=True)
print('deleted')
PythonHost.ir | پایتون هاست
@pythonhost_ir
👍1
کپی فایل با shutil #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import shutil
shutil.copy2('out.txt','out_copy.txt')
print('copied')
PythonHost.ir | پایتون هاست
@pythonhost_ir
کپی پوشه با shutil #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import shutil
shutil.copytree('src','dst', dirs_exist_ok=True)
print('done')
PythonHost.ir | پایتون هاست
@pythonhost_ir
فایل موقت (TemporaryDirectory) #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as d:
p=Path(d)/'a.txt'
p.write_text('hi')
print(p.read_text())
PythonHost.ir | پایتون هاست
@pythonhost_ir
هش متن با SHA256 #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import hashlib
h=hashlib.sha256('hi'.encode()).hexdigest()
print(h)
PythonHost.ir | پایتون هاست
@pythonhost_ir
هش فایل با SHA256 #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import hashlib
from pathlib import Path
p=Path('out.txt')
print(hashlib.sha256(p.read_bytes()).hexdigest())
PythonHost.ir | پایتون هاست
@pythonhost_ir
مقایسه امن رشتهها با hmac #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import hmac
print(hmac.compare_digest('a'*10, 'a'*10))
PythonHost.ir | پایتون هاست
@pythonhost_ir
Base64 encode/decode #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import base64
b=base64.b64encode('سلام'.encode())
print(b.decode())
print(base64.b64decode(b).decode())
PythonHost.ir | پایتون هاست
@pythonhost_ir
فشردهسازی سریع با gzip #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import gzip
data=b'hello'*200
z=gzip.compress(data)
print(len(data),'->',len(z))
PythonHost.ir | پایتون هاست
@pythonhost_ir
ساخت zip داخل حافظه #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import io, zipfile
buf=io.BytesIO()
with zipfile.ZipFile(buf,'w') as z:
z.writestr('a.txt','hello')
print('bytes:', len(buf.getvalue()))
PythonHost.ir | پایتون هاست
@pythonhost_ir