MS python学习(13)

Managing the filesystems

https://learn.microsoft.com/en-us/shows/more-python-for-beginners/managing-the-file-system–more-python-for-beginners-12-of-20

from python 3.6 we are using pathlib instead of os.path

file1

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

module-pathlib

pathlib VS os & os.path

vs

VSC keyboard shortcut for quick edit multiple same part

  1. 鼠标左键选中后,按Ctrl+D 逐个选中相同的部分eg
  2. 鼠标左键选中后,Ctrl+Shift+L 直接选中所有相同部分
  3. 同时选中多个单词,Alt+鼠标左键
  4. 选中文本后,Ctrl + [ 和 Ctrl + ] 可实现文本的向左移动 和 向右移动
  5. 按住Ctrl + Alt,再按键盘的上或下键,可以使一列上出现多个光标
  6. 按Shift+Alt,再使用鼠标拖动,也可以出现竖直的列光标,同时可以选中多列

TO BE CONTINUED…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值