pathlib 学习
import os
import pathlib
from pathlib import Path
import aspose.words as aw
def convert_path(win_path):
tmp_path = win_path.replace(":", "")
tmp_path = tmp_path.replace("\\", "/")
tmp_path = tmp_path.lower()
tmp_path = "/mnt/" + tmp_path
return (tmp_path)
url = pathlib.Path(convert_path(r"C:\Users\xuefe\Downloads\Video"))
files = list(url.glob('*.*'))
# pathlib.PosixPath 转 字符串
for file in files:
file=str(file)
doc = aw.Document(file, aw.loading.LoadOptions("2022"))
doc.save(file)
# file_dir = convert_path(r"C:\Users\xuefe\Downloads\Video")
# files = os.listdir(file_dir)
#
# for file in files:
# file_p=os.path.join(file_dir,file)
# print(type(file_p))
# doc = aw.Document(file_p, aw.loading.LoadOptions("2022"))
# doc.save(file_p)
cwd = pathlib.Path.cwd() # 获取当前目录
home = Path.home() # 获取家目录,比如 /home/ubuntu
path = Path("/mnt/d") # 创建一个新的 path 对象
path.is_dir() # 是否是目录
path.is_file() # 是否是普通文件
path.resolve('.') # 解析成绝对路径,比如 Path(".").resolve() 相当于 Path.cwd()
path.mkdir(parent=True, exists_ok=True) #
path = pathlib.Path(convert_path(r"C:\Users\xuefe\Downloads"))
# 遍历目录
for child in path.iterdir():
print(child)
# 除此之外,还有几个很甜的方法,省去了 with open 语句
text = Path("/home/ubuntu/readme.txt").read_text()
Path("/home/ubuntu/readme.txt").write_text(text)
image = Path("home/ubuntu/image.png").read_bytes()
Path("/home/ubuntu/image.png").write_bytes(image)
评论
发表评论