我有一个从函数调用的路径pathorganizer,如下所示。该功能pathorganizer如下所示,def pathorganizer(raw,output): for root,sub,file in os.walk(raw): for folders in sub: if folders.startswith("raw"): patient_id_raw = root.split(os.sep)[8] for outdir,outsub,outfile in os.walk(output): for folders in outsub: if folders.startswith("raw"): patient_id_output = outdir.split(os.sep)[8] #print(outdir) if folders.startswith("derived"): if patient_id_output == patient_id_raw: path = print(outdir + "/derived/") #path = str(path) return(path) pathorganizer(raw,outdir) /Users/xyz/temp/pro/data_repo/poc/MAY-M-13/PZT-MAY-2/events/derived/ type(pathorganizer(raw,outdir)) /Users/xyz/temp/pro/data_repo/poc/MAY-M-13/PZT-MAY-2/events/derived/ NoneType现在我需要将上述路径(字符串)附加到另一个字符串。上面的字符串被检测为 NoneType,而不是字符串。在这种情况下,我无法将它与另一个字符串连接起来。当我尝试连接时,它抛出以下错误,TypeError: can only concatenate str (not "NoneType") to str这是我尝试过的,for sam in list: file = sam + '.doc' with open(file, 'w') as my_file: my_file.write("whatever: " + pathorganizer(raw,outdir) + sam + "\n")我想知道在这种情况下如何编写或连接我的值。我的目标是.doc写成如下, whatever: /Users/xyz/temp/pro/data_repo/poc/MAY-M-13/PZT-MAY-2/events/derived/sam我也试过了str(pathorganizer(raw,outdir)),然后它返回doc该文件被写为,whatever: Nonesam
1 回答
BIG阳
TA贡献1859条经验 获得超6个赞
如果语句失败,您的pathorganizer函数可以返回。Noneif
在您的情况下,似乎由于print产生了输出,因此if语句已经找到了一些东西。
这里发生了什么:
path = print(outdir + "/derived/")
是print返回None然后:
return(path)正在返回那个None。
因此,无论哪种情况,您的函数都会返回None.
将函数的最后几行更改为:
if folders.startswith("derived"):
if patient_id_output == patient_id_raw:
return outdir + "/derived/"
添加回答
举报
0/150
提交
取消