我有一个 python 脚本,它访问 Windows 操作系统上的映射网络驱动器,并在映射位置创建一个要写入的文件。这是要编写的代码。
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) # Extract the file name from the path
final_filename = os.path.splitext(file_name)[0] # Remove the .bam extension
file.write(f"{final_filename}\t{path}\t{sample_type}\t{processing_setting}\t{reference}\n")
其中,output_dirPath是我正在访问的映射网络驱动器,如下所示
output_dirPath = "//abc/data/lab/Archives/Test/{}".format(directory_name)
目录名称可以是任何包含空格、_ 和 - 的字母数字字符串。
输出后缀为
_template.txt
当我运行此脚本时,代码部分(此处未提及)运行并解压缩文件),但是不会写入文件并给出以下错误。
Traceback (most recent call last):
File "Template.py", line 76, in <module>
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'
open()
(写入)的Python 的
'w'
函数将创建该文件,但这仅在指向该文件的目录路径存在时才有效。路径 //abc/data/lab/Archives/Test/Output Folder abc 1ef3D00.example
不得存在。