os.path.splitext(os.path.basename(in_path.decode("utf8")))如何理解

本文详细介绍了Python中os.path模块的功能及使用方法,包括获取文件名、路径、判断路径有效性等实用函数的应用示例。

1.os.path.basename(in_path.decode(“utf8”))返回最后当前文件名

#coding=utf-8
import os
print os.path.abspath("d:\\new\\test.txt")    #d:\new\test.txt
print os.path.basename("d:\\new\\test.txt")   #test.txt
print os.path.dirname("d:\\new\\test.txt")    #d:\new
print os.path.exists("d:\\new")               #True
print os.path.lexists("d:\\new")              #True
print os.path.expanduser("d:\\new\\text.txt") #d:\new\text.txt
print os.path.getatime("d:\\new")  #最后访问时间
print os.path.getmtime("d:\\new") #最后修改路径时间
print os.path.getctime("d:\\new")  #创建时间
print os.path.getsize("d:\\new\\")  #或许路径的大小 字节为单位
print os.path.isabs("d:\\")  #是否是绝对路径
print os.path.isfile("d:\\new\\hello.txt")
print os.path.isdir("d:\\new")
print os.path.islink("d:\\new\\hello.txt")
print os.path.join("d:\\new","hello.txt")
print os.path.normcase("d:\\new\\hello.txt")
print os.path.relpath("d:\\new\\hello.txt")  #相对路径
print os.path.split("d:\\new\\hello.txt")  #分离文件名
print os.path.splitdrive("d:\\new\\hello.txt")  #分离磁盘驱动器
print os.path.splitext("d:\\new\\hello.txt")  #分离扩展名

os.path.splitext()

split()  用于返回目录路径和文件名的元组
import os
os.path.split('d:\\library\\book.txt') #输出('d:\\library', 'book.txt')

splitdrive()    用于返回盘符和路径字符元组
os.path.splitdrive('d:\\library\\book.txt') #输出('d:', '\\library\\book.txt')

splitext()    用于返回文件名和扩展名元组
os.path.splitext('d:\\library\\book.txt')('d:\\library\\book', '.txt')
os.path.splitext('book.txt')('book', '.txt')

所以os.path.splitext(os.path.basename(in_path.decode(“utf8”)))有什么作用

os.path.basename("d:\\new\\book.txt") 
os.path.splitext('book.txt')('book', '.txt')

能够得到当前路径下的文件名~

import sys import os import tempfile import subprocess import time from PyQt5.QtWidgets import ( QApplication, QMainWindow, QPushButton, QTextEdit, QFileDialog, QVBoxLayout, QWidget, QStatusBar, QProgressDialog ) from PyQt5.QtCore import Qt, QThread, pyqtSignal from docx import Document class DocumentReaderApp(QMainWindow): def init(self): super().init() self.setWindowTitle(“文档阅读器”) self.setGeometry(100, 100, 800, 600) # 主布局 main_widget = QWidget() self.setCentralWidget(main_widget) layout = QVBoxLayout(main_widget) # 选择文件按钮 self.btn_open = QPushButton("选择文件") self.btn_open.clicked.connect(self.open_file) layout.addWidget(self.btn_open) # 文本显示区域 self.text_edit = QTextEdit() self.text_edit.setReadOnly(True) layout.addWidget(self.text_edit) # 状态栏 self.status_bar = QStatusBar() self.setStatusBar(self.status_bar) self.status_bar.showMessage("就绪") # 检查LibreOffice是否安装 self.libreoffice_path = self.find_libreoffice() if not self.libreoffice_path: self.status_bar.showMessage("警告: 未找到LibreOffice,DOC/WPS文件转换功能不可用") def find_libreoffice(self): """查找系统中安装的LibreOffice""" # Windows路径 windows_paths = [ r"C:\Program Files\LibreOffice\program\soffice.exe", r"C:\Program Files (x86)\LibreOffice\program\soffice.exe" ] # Linux路径 linux_paths = [ "/usr/bin/libreoffice", "/usr/bin/soffice", "/snap/bin/libreoffice" ] # 检查路径是否存在 paths = windows_paths if sys.platform == "win32" else linux_paths for path in paths: if os.path.exists(path): return path # 尝试通过PATH查找 try: if sys.platform == "win32": result = subprocess.run(["where", "soffice"], capture_output=True, text=True) else: result = subprocess.run(["which", "soffice"], capture_output=True, text=True) if result.returncode == 0 and os.path.exists(result.stdout.strip()): return result.stdout.strip() except: pass return None def open_file(self): """打开文件对话框并读取内容""" file_path, _ = QFileDialog.getOpenFileName( self, "选择文档", "", "文档文件 (*.docx *.doc *.wps);;所有文件 (*.*)" ) if not file_path: return self.status_bar.showMessage(f"正在处理: {os.path.basename(file_path)}...") QApplication.processEvents() # 更新UI try: text = self.read_document(file_path) self.text_edit.setText(text) self.status_bar.showMessage(f"成功读取: {os.path.basename(file_path)}") except Exception as e: self.text_edit.setText(f"错误: {str(e)}") self.status_bar.showMessage(f"读取失败: {os.path.basename(file_path)}") def read_document(self, file_path): """根据文件类型选择读取方法""" ext = os.path.splitext(file_path)[1].lower() if ext == '.docx': return self.read_docx(file_path) elif ext in ('.doc', '.wps'): if not self.libreoffice_path: raise RuntimeError("未安装LibreOffice,无法转换DOC/WPS文件") # 显示进度对话框 progress = QProgressDialog("正在转换文件...", "取消", 0, 0, self) progress.setWindowTitle("文档转换") progress.setWindowModality(Qt.WindowModal) progress.setCancelButton(None) # 禁用取消按钮 progress.show() QApplication.processEvents() # 转换文件 converted_file = self.convert_to_docx(file_path) # 关闭进度对话框 progress.close() # 读取转换后的文件 return self.read_docx(converted_file) else: raise ValueError("不支持的格式") def convert_to_docx(self, file_path): """使用LibreOffice将文件转换为DOCX格式""" # 创建临时目录 temp_dir = tempfile.mkdtemp() try: # 构建转换命令 cmd = [ self.libreoffice_path, "--headless", # 无界面模式 "--convert-to", "docx", # 转换为docx "--outdir", temp_dir, # 输出目录 file_path # 输入文件 ] # 执行转换 result = subprocess.run( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=60 # 超时60秒 ) if result.returncode != 0: error_msg = result.stderr.decode('utf-8', errors='ignore') raise RuntimeError(f"文件转换失败: {error_msg}") # 查找转换后的文件 base_name = os.path.splitext(os.path.basename(file_path))[0] converted_path = os.path.join(temp_dir, f"{base_name}.docx") if not os.path.exists(converted_path): raise RuntimeError("转换后的文件未找到") return converted_path except subprocess.TimeoutExpired: raise RuntimeError("文件转换超时") except Exception as e: raise RuntimeError(f"转换过程中出错: {str(e)}") def read_docx(self, file_path): """读取.docx文件""" try: doc = Document(file_path) text = '\n'.join([para.text for para in doc.paragraphs]) # 如果是临时文件,读取后删除 if "tmp" in file_path.lower() or "temp" in file_path.lower(): try: os.remove(file_path) temp_dir = os.path.dirname(file_path) if os.path.exists(temp_dir) and not os.listdir(temp_dir): os.rmdir(temp_dir) except: pass return text except Exception as e: raise RuntimeError(f"读取DOCX文件失败: {str(e)}") if name == “main”: app = QApplication(sys.argv) window = DocumentReaderApp() window.show() sys.exit(app.exec_()) 这个代码可以正常运行并读取doc、docx、wps文件,但是不能正确识别word自动生成的编号,请帮我解决这个问题,并生成完整的代码
06-16
import os import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler import zipfile import hashlib class ZipHandler(FileSystemEventHandler): def __init__(self, target_dir): self.target_dir = target_dir self.processed_files = set() def get_file_hash(self, file_path): with open(file_path, 'rb') as f: return hashlib.md5(f.read()).hexdigest() def on_created(self, event): if event.is_directory: return if event.src_path.endswith('.zip'): file_hash = self.get_file_hash(event.src_path) if file_hash not in self.processed_files: print(f"检测到新压缩包: {event.src_path}") self.unzip_file(event.src_path) self.processed_files.add(file_hash) else: print(f"已处理过该文件: {event.src_path}") def unzip_file(self, file_path): try: base_name = os.path.splitext(os.path.basename(file_path))[0] dest_dir = os.path.join(self.target_dir, base_name) os.makedirs(dest_dir, exist_ok=True) with zipfile.ZipFile(file_path, 'r') as zip_ref: zip_ref.extractall(dest_dir) print(f"解压完成 -> {dest_dir}") except Exception as e: print(f"解压失败: {e}") def monitor_folder(watch_dir, target_dir): event_handler = ZipHandler(target_dir) observer = Observer() observer.schedule(event_handler, watch_dir, recursive=False) observer.start() print(f"开始监控: {watch_dir}") try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() if __name__ == "__main__": WATCH_DIR = r"D:\Users\23118\Desktop\测试二" TARGET_DIR = r"D:\Users\23118\Desktop\测试二" os.makedirs(WATCH_DIR, exist_ok=True) monitor_folder(WATCH_DIR, TARGET_DIR) 为什么我这个代码不行,帮我分析以下
08-16
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值