Managing the filesystems
from python 3.6 we are using pathlib instead of os.path
some useful commands
# python3.6 and above to use pathlib other than os.path
from pathlib import Path
# pathes related
print('******path related******')
cwd = Path.cwd()
print(cwd)
file_path = Path.joinpath(cwd, 'mixins.py')
print(file_path)
print(f'Does {file_path} exist? {file_path.exists()}')
# directory related
print('******director related******')
parent = cwd.parent
print(f'Parent folder: {parent}')
print(f'is {parent} a folder? {parent.is_dir()}')
print(f'is {parent} a file? {parent.is_file()}')
for child in parent.iterdir():
if child.is_file():
print(child)
# files related
print('******files related******')
print(f'file name: {file_path.name}')
print(f'file extention: {file_path.suffix}')
print(f'parent of file: {file_path.parent.name}')
print(f'file size: {file_path.stat().st_size}KB')
output:
******path related******
C:\Users\meij1\Videos\MS
C:\Users\meij1\Videos\MS\mixins.py
Does C:\Users\meij1\Videos\MS\mixins.py exist? True
******director related******
Parent folder: C:\Users\meij1\Videos
is C:\Users\meij1\Videos a folder? True
is C:\Users\meij1\Videos a file? False
C:\Users\meij1\Videos\desktop.ini
******files related******
file name: mixins.py
file extention: .py
parent of file: MS
Traceback (most recent call last):
File "C:\Users\meij1\Videos\MS\file_example1.py", line 31, in <module>
print(f'file size: {file_path.stat.st_size}')
AttributeError: 'function' object has no attribute 'st_size'
PS C:\Users\meij1\Videos\MS> python .\file_example1.py
******path related******
C:\Users\meij1\Videos\MS\mixins.py
Does C:\Users\meij1\Videos\MS\mixins.py exist? True
******director related******
Parent folder: C:\Users\meij1\Videos
is C:\Users\meij1\Videos a folder? True
is C:\Users\meij1\Videos a file? False
C:\Users\meij1\Videos\desktop.ini
******files related******
file name: mixins.py
file extention: .py
parent of file: MS
file size: 774
PS C:\Users\meij1\Videos\MS> python .\file_example1.py
******path related******
C:\Users\meij1\Videos\MS
C:\Users\meij1\Videos\MS\mixins.py
Does C:\Users\meij1\Videos\MS\mixins.py exist? True
******director related******
Parent folder: C:\Users\meij1\Videos
is C:\Users\meij1\Videos a folder? True
is C:\Users\meij1\Videos a file? False
C:\Users\meij1\Videos\desktop.ini
******files related******
file name: mixins.py
file extention: .py
parent of file: MS
file size: 774KB
For the pathlib library
pathlib VS os & os.path
VSC keyboard shortcut for quick edit multiple same part
- 鼠标左键选中后,按Ctrl+D 逐个选中相同的部分
- 鼠标左键选中后,Ctrl+Shift+L 直接选中所有相同部分
- 同时选中多个单词,Alt+鼠标左键
- 选中文本后,Ctrl + [ 和 Ctrl + ] 可实现文本的向左移动 和 向右移动
- 按住Ctrl + Alt,再按键盘的上或下键,可以使一列上出现多个光标
- 按Shift+Alt,再使用鼠标拖动,也可以出现竖直的列光标,同时可以选中多列