Files
adii1823 48cef734e0 ch08
2021-10-28 17:39:37 +05:30

36 lines
817 B
Python

# files/ops_create.py
import shutil
from pathlib import Path
base_path = Path('ops_example')
# let's perform an initial cleanup just in case
if base_path.exists() and base_path.is_dir():
shutil.rmtree(base_path)
# now we create the directory
base_path.mkdir()
path_b = base_path / 'A' / 'B'
path_c = base_path / 'A' / 'C'
path_d = base_path / 'A' / 'D'
path_b.mkdir(parents=True)
path_c.mkdir() # no need for parents now, as 'A' has been created
# we add three files in `ops_example/A/B`
for filename in ('ex1.txt', 'ex2.txt', 'ex3.txt'):
with open(path_b / filename, 'w') as stream:
stream.write(f'Some content here in {filename}\n')
shutil.move(path_b, path_d)
# we can also rename files
ex1 = path_d / 'ex1.txt'
ex1.rename(ex1.parent / 'ex1.renamed.txt')
# now call $ tree ops_example