python路径问题

在open函数中使用相对路径...总是提示文件不存在。于是便使用绝对路径了。

获取绝对路径的代码:

python 复制代码
        script_dir = os.path.dirname(os.path.abspath(__file__))
        self.parent_dir = os.path.dirname(script_dir)

python这个坑,挺害人的呀。记录下来,浪费了好多的时间。

Flask处理文件上传:

python 复制代码
@app.route('/upload', methods = ['POST'])
def save_file():
   if 'file' not in request.files:
      return 'No file part'
   file = request.files['file']
   if file.filename == '':
      return 'No selected file'
   if file:
      file.save(os.path.join("upload_folder", file.filename))
      return 'File ' + file.filename + ' successfully uploaded'

windows/linux文件锁:

python 复制代码
os_name = platform.system()
if os_name == "Windows":
    import msvcrt
    # 锁定文件
    msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 0)  # 锁定文件
    # 写入数据
    pickle.dump(global_resource, f)
    # 解锁文件
    msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 0)  # 解锁文件
else:
    import fcntl
    fcntl.flock(f, fcntl.LOCK_EX)
    pickle.dump(global_resource, f)
    fcntl.flock(f, fcntl.LOCK_UN)
相关推荐
码云骑士3 小时前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
闵孚龙4 小时前
《PyTorch 深度修炼》Dataset 和 DataLoader:数据如何喂给模型
人工智能·pytorch·python
goldenrolan4 小时前
A公司物料替代测试系统 v1.7:从需求到 exe/apk 的 AI 辅助全链路实践
android·自动化测试·软件测试·python·ai
菜板春4 小时前
jupyter入门-手册-特征探索
python·jupyter
Metaphor6925 小时前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
极光代码工作室5 小时前
基于数据仓库的电商数据分析平台
大数据·hadoop·python·spark·数据可视化
开发小能手-roy5 小时前
StringBuilder vs StringBuffer:2024年还需要线程安全字符串吗?
开发语言·python·安全
AC赳赳老秦5 小时前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
2601_954706495 小时前
云手机技术详解+Python实战调用|2026高稳云手机平台推荐
开发语言·python·智能手机