This is a memo while studying python I will add various things in the future
# Open and close the file
f = open('text.text', 'w')
f.write('Text\n')
f.close()
with open('text.text', 'w') as f:
f.write('Test\n')
# By chunk
with open('text.text', 'r') as f:
while True:
chunk = 2
line = f.read(chunk)
print(line)
if not line:
break
>>Te
>>st
(Test is entered in the text.text file)
s = """\
AAA
BBB
CCC
"""
with open('text.text', 'r') as f:
print(f.tell())
print(f.read(1))
f.seek(5)
>>>0
>>>A
s = """\
AAA
BBB
CCC
EEE
"""
with open('text.text', 'r+') as f:
print(f.read())
f.seek(0)
f.write(s)
# Also written to the text.text file
AAA
BBB
CCC
EEE
import string
s = """\
Hi $name
$contents
Have a good day
"""
t = string.Template(s)
contents = t.substitute(name='Mike', contents='How are you')
print(contents)
>>>Hi Mike
>>>How are you
>>>Have a good day
import csv
with open('text.csv,', 'w') as csv_file:
fieldnames = ['Name', 'Count']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'Name': 'A', 'Count': 1})
writer.writerow({'Name': 'B', 'Count': 2})
with open('test.csv', 'r') as csv_file:
reader = csv.DictReader(csv)
for row in reader:
print(row['Name'], row['Count'])
import os
import pathlib
import glob
import shutil
# Whether there is text.text
print(os.path.exists('text.text'))
>>>True
# Check if it is a file
print(os.path.isfile('text.text'))
# Is it a directory?
print(print(os.path.isdir('sample_codes')))
# Rename the file
os.rename('text.text', 'renamed.text')
# Simulink link
os.symlink('renamed.text', 'symlink.txt')
# Create a directory
os.mkdir('test.dir')
# Erase the directory
os.rmdir('test.dir')
# Create a file
pathlib.Path('empty.txt').touch()
# Erase files
os.remove('empty.txt')
# View the list of directories
os.mkdir('test_dir')
os.mkdir('test_dir/test_dir2')
print(os.listdir('test_dir'))
# What kind of files are there
pathlib.Path('test_dir/test_dir2/empty.txt').touch()
# View all files
print(glob.glob('test_dir/test_dir2/*'))
# make a copy
shutil.copy('test_dir/test_dir2/empty.txt','test_dir/test_dir2/empty2.txt')
# Erase all directories
shutil.rmtree('test_dir')
# I want to know the location of the directory
print(os.getcwd())
import tarfile
with tarfile.open('test.tar.gz', 'w:gz') as tr:
tr.add('test_dir')
# Deploy with python
with tarfile.open('test.tar.gz', 'r:gz') as tr:
tr.extractall(path='test_dir')
# If you want to see only the contents
with tarfile.open('test.tar.gz', 'r:gz') as tr:
with tr.extractall('test_dir/sub_dir/sub_test.txt') as f:
print(f.read())
import glob
import zipfile
with zipfile.ZipFile('text.zip', 'w') as z:
z.write('test_dir')
z.write('test_dir/text.txt')
for f in glob.glob('text_dir/**', recursive=True):
z.write(f)
with zipfile.ZipFile('test.zip', 'r') as z:
z.extractall('zzz2')
with z.open('text_dir/text.txt') as f:
print(f.read())
import tempfile
# Temporarily generate a file
with tempfile.TemporaryFile(mode='w+') as t:
t.write('hello')
t.seek(0)
print(t.read())
# Actually create the file
with tempfile.NamedTemporaryFile(delete=False) as t:
print(t.name)
with open(t.name, '+w') as f:
f.write('test\n')
f.seek(0)
print(f.read())
with tempfile.TemporaryDirectory() as td:
print(td)
temp_dir = tempfile.mkdtemp()
print(temp_dir)
import subprocess
subprocess.run(['ls', '-al'])
import datetime
now = datetime.datetime.now()
print(now)
print(now.isoformat())
print(now.strftime('%d/%m/%y'))
today = datetime.date.today()
print(today.isoformat())
print(today.strftime('%d/%m/%y'))
Recommended Posts