[Python进阶] 路径相关库:os、pathlib、shutil

6.10 路径相关:os、pathlib、shutil

6.10.1 返回当前用户目录

from icecream import ic
import os
from pathlib import Path

ic(Path.home())
ic(os.path.expanduser('~'))

11:21:22|> Path.home(): WindowsPath(‘C:/Users/frb’)
11:21:22|> os.path.expanduser(‘~’): ‘C:\Users\frb’

6.10.2 返回绝对路径

from icecream import ic
import os
from pathlib import Path

path = Path('.')
ic(path.resolve())
ic(os.path.abspath('.'))
ic(os.path.abspath('..'))

14:50:06|> path.resolve(): WindowsPath(‘E:/Syncdisk/PythonFiles’)
14:50:06|> os.path.abspath(‘…’): ‘E:\Syncdisk’
14:50:06|> os.path.abspath(‘.’): ‘E:\Syncdisk\PythonFiles’

6.10.3 返回文件的目录

from icecream import ic
import os
from pathlib import Path

path1 = Path(r'G:\TCL\Temp\log.txt')
ic(path1.parent)
path2 = Path('./t1.py')
ic(path2.parent)
ic(os.path.dirname(r'G:\TCL\Temp\log.txt'))
ic(os.path.dirname('./t1.py'))

14:54:08|> path1.parent: WindowsPath(‘G:/TCL/Temp’)
14:54:08|> path2.parent: WindowsPath(‘.’)
14:54:08|> os.path.dirname(r’G:\TCL\Temp\log.txt’): ‘G:\TCL\Temp’
14:54:08|> os.path.dirname(‘./t1.py’): ‘.’

6.10.4 返回文件名

from icecream import ic
import os
from pathlib import Path

path1 = Path(r'G:\TCL\Temp\log.txt')
ic(path1.name)
path2 = Path('./t1.py')
ic(path2.name)
ic(os.path.basename(r'G:\TCL\Temp\log.txt'))
ic(os.path.basename('./t1.py'))

14:56:54|> path1.name: ‘log.txt’
14:56:54|> path2.name: ‘t1.py’
14:56:54|> os.path.basename(r’G:\TCL\Temp\log.txt’): ‘log.txt’
14:56:54|> os.path.basename(‘./t1.py’): ‘t1.py’

6.10.5 返回文件扩展名

from icecream import ic
from pathlib import Path

path1 = Path(r'G:\TCL\Temp\log.txt')
ic(path1.suffix)
path2 = Path('./t1.py')
ic(path2.suffix)

15:00:58|> path1.suffix: ‘.txt’
15:00:58|> path2.suffix: ‘.py’

6.10.6 返回带路径短文件名和扩展名

from icecream import ic
import os

ic(os.path.splitext(r'G:\TCL\Temp\log.txt'))
ic(os.path.splitext('./t1.py'))

15:00:58|> os.path.splitext(r’G:\TCL\Temp\log.txt’): (‘G:\TCL\Temp\log’, ‘.txt’)
15:00:58|> os.path.splitext(‘./t1.py’): (‘./t1’, ‘.py’)

6.10.7 返回连接的路径

from icecream import ic
import os
from pathlib import Path

path1 = Path(r'G:\TCL\Temp')
ic(path1)
path2 = Path('t1.py')
ic(path2)
ic(path1.joinpath(path1, path2))
ic(Path.joinpath(path1, path2))
ic(Path(path1, path2))
ic(Path(r'G:\TCL\Temp', 't1.py'))
ic(os.path.join(r'G:\TCL\Temp', 'log.txt'))

15:19:10|> path1: WindowsPath(‘G:/TCL/Temp’)
15:19:10|> path2: WindowsPath(‘t1.py’)
15:19:10|> path1.joinpath(path1, path2): WindowsPath(‘G:/TCL/Temp/t1.py’)
15:19:10|> Path.joinpath(path1, path2): WindowsPath(‘G:/TCL/Temp/t1.py’)
15:19:10|> Path(path1, path2): WindowsPath(‘G:/TCL/Temp/t1.py’)
15:19:10|> Path(r’G:\TCL\Temp’, ‘t1.py’): WindowsPath(‘G:/TCL/Temp/t1.py’)
15:19:10|> os.path.join(r’G:\TCL\Temp’, ‘log.txt’): ‘G:\TCL\Temp\log.txt’

6.10.8 判断路径是否存在

from icecream import ic
import os
from pathlib import Path

f1 = r'G:\TCL\Temp\log.txt'
f2 = r'G:\TCL\Temp\log.py'
p1 = Path(f1)
p2 = Path(f2)
ic(p1.exists())
ic(p2.exists())
ic(os.path.exists(f1))
ic(os.path.exists(f2))

15:28:53|> p1.exists(): True
15:28:53|> p2.exists(): False
15:28:53|> os.path.exists(f1): True
15:28:53|> os.path.exists(f2): False

6.10.9 判断两个路径是否相同

from icecream import ic
import os
from pathlib import Path

path1 = Path(r'G:\TCL\Temp\log2.txt')
path2 = Path('t1.py')
path3 = Path(r'E:\Syncdisk\PythonFiles\t1.py')
ic(Path.samefile(path1, path2))  # 路径不存在会报错
ic(Path.samefile(path2, path3))
ic(os.path.samefile(path1, path2))  # 路径不存在会报错
ic(os.path.samefile(path2, path3))

15:24:54|> Path.samefile(path1, path2): False
15:24:54|> Path.samefile(path2, path3): True
15:24:54|> os.path.samefile(path1, path2): False
15:24:54|> os.path.samefile(path2, path3): True

6.10.10 判断路径是否为符号连接

首先,先用批处理创建出2个连接文件,一个是符号连接,另一个是硬连接。

mklink loglink.txt log.txt
为 loglink.txt <<===>> log.txt 创建的符号链接
mklink /H log_hard.txt log.txt
为 log_hard.txt <<===>> log.txt 创建了硬链接

这样就为log.txt创建了2个连接文件,符号链接名为:loglink.txt,而硬连接名为:log_hard.txt。

from icecream import ic
import os
from pathlib import Path

f1 = r'G:\TCL\Temp\log.txt'
f2 = r'G:\TCL\Temp\log.txt.lnk'
f3 = r'G:\TCL\Temp\loglink.txt'
f4 = r'G:\TCL\Temp\log_hard.txt'
p1 = Path(f1)
p2 = Path(f2)
p3 = Path(f3)
p4 = Path(f4)
ic(p1.is_symlink())
ic(p2.is_symlink())
ic(p3.is_symlink())
ic(p4.is_symlink())
ic(os.path.islink(f1))
ic(os.path.islink(f2))
ic(os.path.islink(f3))
ic(os.path.islink(f4))

15:37:35|> p1.is_symlink(): False
15:37:35|> p2.is_symlink(): False
15:37:35|> p3.is_symlink(): True
15:37:35|> p4.is_symlink(): False
15:37:35|> os.path.islink(f1): False
15:37:35|> os.path.islink(f2): False
15:37:35|> os.path.islink(f3): True
15:37:35|> os.path.islink(f4): False

6.10.11 判断路径是否为绝对路径

Path.is_absolute()
os.path.isabs()

6.10.12 判断路径是否为目录

Path.is_dir()
os.path.isdir()

6.10.13 判断路径是否为文件

Path.is_file()
os.path.isfile()

你可能感兴趣的:(Python进阶,#,六,系统及相关操作,python,Python进阶,系统路径)