[英]Python - Problem with writing file Errno 2
I am a Python newbie and I am having a problem that probably has as a simple answer. 我是Python新手,可能遇到的问题很简单。 I have the following script, which works most of the way, I just get stuck trying the write the output file. 我有以下脚本,该脚本在大多数情况下都有效,只是尝试写输出文件而卡住了。 The error I get is at the very end: IOError: [Errno 2] No such file or directory: '/D/1_NEW_ANALYSIS/Scripts/Melodic_fsfs/design_Rat01_Run_1.fsf' 我得到的错误在最后:IOError:[Errno 2]没有这样的文件或目录:'/D/1_NEW_ANALYSIS/Scripts/Melodic_fsfs/design_Rat01_Run_1.fsf'
Here is the code: 这是代码:
import os
import glob
studydir = 'D:/1_NEW_ANALYSIS'
fsfdir="%s/Scripts/Melodic_fsfs"%(studydir)
templatedir="%s/Scripts/Templates"%(studydir)
subdirs=glob.glob("%s/Subjects/Rat_[0-9][0-9]/Run_[0-2]"%(studydir))
for dir in list(subdirs):
splitdir = dir.split('\\')
# YOU WILL NEED TO EDIT THIS TO GRAB sub001
splitdir_sub = splitdir[1]
subnum=splitdir_sub[-2:]
splitdir_run = splitdir[2]
runnum=splitdir_run[-1:]
print(subnum)
replacements = {'SUBNUM':subnum, 'RUNNUM':runnum}
with open("%s/Melodic_design.fsf"%(templatedir)) as infile:
with open("%s/design_Rat%s_Run_%s.fsf"%(fsfdir, subnum, runnum), 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)
Anybody have an idea why it doesn't work? 有人知道为什么它不起作用吗?
Thanks a lot! 非常感谢!
如果您在Windows上运行(我想是的),studydir应该如下所示:
studydir = 'D:\\1_NEW_ANALYSIS'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.