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)
相关推荐
卷无止境4 分钟前
WebGIS生态全景丨从浏览器里的地图到背后的空间数据库
后端·python
Y38153266210 分钟前
竞品上新监控:用搜索 API 盯住对手发布了什么新功能
python·搜索引擎
benchmark_cc10 分钟前
如何设计一个 Python 实时行情获取程序?从轮询、数据处理到量化策略接入
python·数据分析·pandas·量化交易·股票数据·quantdash
Metaphor69220 分钟前
Python 实现 Word/TXT 互转:附完整代码
python·word·格式转换·txt
柒和远方42 分钟前
DocResearch 项目面试:把每个模块讲明白,而不是背术语
python·llm·agent
信誓旦旦的程序猿44 分钟前
【Python 量化取数指南 #13】Python 把行情落库:sqlite 一键存,回测随用随取
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据api
LensonYuan1 小时前
Cursor状态栏一键切换OpenAI API Key
人工智能·python·cursor
柒和远方1 小时前
DocResearch 项目理解:从一条命令到一份有出处的报告
python·llm·agent
北冥有鱼被烹1 小时前
NVIDIA DGX/HGX 服务器全景深度解析:从 H100 到 GB300,专业算力选型必读
运维·服务器·网络·python
Maiko Star1 小时前
* LangChain Agent智能体详解(上):创建调用、工具绑定与系统提示词
python·langchain·agent