You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下Python脚本无法写入映射网络驱动器问题求助

Windows下Python访问映射网络驱动器写入文件失败

在Windows系统中运行Python脚本,访问映射网络驱动器并创建文件写入时失败,但脚本中的解压文件代码可正常执行。

相关代码

输出目录路径定义

output_dirPath = "//abc/data/lab/Archives/Test/{}".format(directory_name)

其中directory_name为包含空格、下划线、短横线的任意字母数字字符串,输出后缀output_suffix为_template.txt。

文件写入代码

output_file = f"{output_dirPath}/{directory_name}{output_suffix}"

with open(output_file, 'w') as file:
    file.write("Sample Name\tFilename\tSample Type\tProcessing Setting\tReference\n")
    for path in file_paths:
        file_name = os.path.basename(path)  # 从路径中提取文件名
        final_filename = os.path.splitext(file_name)[0]  # 移除.bam扩展名
        file.write(f"{final_filename}\t{path}\t{sample_type}\t{processing_setting}\t{reference}\n")

报错信息

Traceback (most recent call last):
File "Template.py", line 76, in
with open(output_file, 'w') as file:
^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '//abc/data/lab/Archives/Test/Output Folder abc 1ef3D00.example/Output Folder abc 1ef3D00.example_bamTemplate.txt'

问题原因及解决办法

可能原因

  1. 路径拼接错误:手动用/拼接路径容易出现格式问题,尤其是Windows系统下的网络路径,open函数可能无法正确解析。
  2. 目标目录不存在:虽然解压代码正常,但写入的目标目录可能未被创建,open函数不会自动创建目录。
  3. 权限不足:网络驱动器的目标目录可能没有写入权限。

解决办法

  1. 使用专业路径处理工具:用os.path或pathlib模块构建路径,避免手动拼接出错:
    • 用os.path的示例:
      import os
      
      # 构建基础目录和目标目录路径
      base_dir = "//abc/data/lab/Archives/Test"
      output_dirPath = os.path.join(base_dir, directory_name)
      # 确保目录存在,已存在则不报错
      os.makedirs(output_dirPath, exist_ok=True)
      # 构建完整文件路径
      output_file = os.path.join(output_dirPath, f"{directory_name}{output_suffix}")
      
      # 写入文件
      with open(output_file, 'w') as file:
          file.write("Sample Name\tFilename\tSample Type\tProcessing Setting\tReference\n")
          for path in file_paths:
              file_name = os.path.basename(path)
              final_filename = os.path.splitext(file_name)[0]
              file.write(f"{final_filename}\t{path}\t{sample_type}\t{processing_setting}\t{reference}\n")
      
    • 用pathlib的示例(更推荐):
      from pathlib import Path
      
      base_dir = Path("//abc/data/lab/Archives/Test")
      output_dirPath = base_dir / directory_name
      # 创建目录,包含父目录且已存在则不报错
      output_dirPath.mkdir(parents=True, exist_ok=True)
      output_file = output_dirPath / f"{directory_name}{output_suffix}"
      
      # 写入文件
      with open(output_file, 'w') as file:
          file.write("Sample Name\tFilename\tSample Type\tProcessing Setting\tReference\n")
          for path in file_paths:
              file_name = os.path.basename(path)
              final_filename = os.path.splitext(file_name)[0]
              file.write(f"{final_filename}\t{path}\t{sample_type}\t{processing_setting}\t{reference}\n")
      
  2. 确保目录存在:在写入文件前,必须确认目标目录已创建,上述代码中的os.makedirs和pathlib.Path.mkdir都能实现这一点。
  3. 验证权限:手动在网络驱动器的目标目录下创建文件,确认是否有写入权限。若没有,需调整网络共享的权限设置。

内容的提问来源于stack exchange,提问作者Callie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:32:20