<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 文件系统相关操作
pathlib
The pathlib module was introduced in Python 3.4
比string 类型path 能提供更灵活功能
cheat sheet
Step1:  useful functions
.read_text()
Step2:  .name
Step3:  Find the Last Modified File
Step4:  Create a Unique File Name
Step5:  dir exist and then glob with multiple extensions
Step6:  shutil
Step7:  collections Counter
<ASSISTANT_TASK:>
Python Code:
from pathlib import Path
import pathlib
save_dir = "./test_dir"
Path(save_dir).mkdir(parents=True, exist_ok=True)
### get current directory
print(Path.cwd())
print(Path.home())
print(pathlib.Path.home().joinpath('python', 'scripts', 'test.py'))
Explanation: 文件系统相关操作
pathlib
The pathlib module was introduced in Python 3.4
比string 类型path 能提供更灵活功能
cheat sheet: https://github.com/chris1610/pbpython/blob/master/extras/Pathlib-Cheatsheet.pdf
shutil
文件夹操作
pathlib  创建文件夹，如果不存在
End of explanation
# Reading and Writing Files
path = pathlib.Path.cwd() / 'test.txt'
with open(path, mode='r') as fid:
    headers = [line.strip() for line in fid if line.startswith('#')]
print('\n'.join(headers))
print('full text', path.read_text())
print(path.resolve().parent == pathlib.Path.cwd())
Explanation: useful functions
.read_text(): open the path in text mode and return the contents as a string.
.read_bytes(): open the path in binary/bytes mode and return the contents as a bytestring.
.write_text(): open the path and write string data to it.
.write_bytes(): open the path in binary/bytes mode and write data to it.
.resolve() method will find the full path.
End of explanation
print('path', path)
print('stem', path.stem)
print('suffix', path.suffix)
print('parent', path.parent)
print('parent of parent', path.parent.parent)
print('anchor', path.anchor)
# move or replace file
path.with_suffix('.py')
path.replace(path.with_suffix('.md')) # 改后缀
path.with_suffix('.md').replace(path.with_suffix('.txt'))
# Display a Directory Tree
def tree(directory):
    print(f'+ {directory}')
    for path in sorted(directory.rglob('*')):
        depth = len(path.relative_to(directory).parts)
        spacer = '    ' * depth
        print(f'{spacer}+ {path.name}')
tree(pathlib.Path.cwd())
Explanation: .name: the file name without any directory
.parent: the directory containing the file, or the parent directory if path is a directory
.stem: the file name without the suffix
.suffix: the file extension
.anchor: the part of the path before the directories
End of explanation
from datetime import datetime
directory = pathlib.Path.cwd()
time, file_path = max((f.stat().st_mtime, f) for f in directory.iterdir())
print(datetime.fromtimestamp(time), file_path)
directory = pathlib.Path.home()
file_list = list(directory.glob('*.*'))
print(file_list)
Explanation: Find the Last Modified File
End of explanation
def unique_path(directory, name_pattern):
    counter = 0
    while True:
        counter += 1
        path = directory / name_pattern.format(counter)
        if not path.exists():
            return path
path = unique_path(pathlib.Path.cwd(), 'test{:03d}.txt')
print(path)
Explanation: Create a Unique File Name
End of explanation
input_path = Path("/mnt/d/code/image/hedian-demo/data/test/220425")
file_list = []
if input_path.exists():
    if input_path.is_dir():
        # for a in input_path.glob("*"):
        #     print(a)
        file_list = [p.resolve() for p in input_path.glob("*") if
                 p.suffix in {".png", ".jpg", ".JPG", ".PNG"}]
        print(len(file_list), file_list)
    else:
        print(p)
# PosixPath as str: str(p.resolve())
Explanation: dir exist and then glob with multiple extensions
End of explanation
# move all .txt file to achive fold
import glob
import os
import shutil
for file_name in glob.glob('*.txt'): # return a list of 
    new_path = os.path.join('archive', file_name)
    shutil.move(file_name, new_path)
Explanation: shutil
End of explanation
# counting files
import collections
print(collections.Counter(p.suffix for p in pathlib.Path.cwd().iterdir()))
print('漂亮', collections.Counter(p.suffix for p in pathlib.Path.cwd().glob('*.t*')))
Explanation: collections Counter
End of explanation
<END_TASK>