我在创建一个目录后,无法打开/创建/写入指定目录中的文件,原因不明。我正在使用os.mkdir()和
path=chap_name
print "Path : "+chap_path #For debugging purposes
if not os.path.exists(path):
os.mkdir(path)
temp_file=open(path+'/'+img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
我遇到了错误:
OSError: [Errno 2] No such file or directory: 'Some Path Name'
路径的形式是'带有未转义空格的文件夹名称'
这里我做错了什么?
更新:我尝试在没有创建目录的情况下运行代码。
path=chap_name
print "Path : "+chap_path #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
仍然出现错误。更加困惑。
更新2:问题似乎在于img_alt,它在某些情况下包含'/',这是造成问题的原因。
所以我需要处理'/'。 有没有办法转义'/'或者只能删除?
os.path.join()
更好,将代码改为:os.path.join(path, img_alt+'.jpg')
。 - Levonpath
гҖҒchap_path
е’Ңimg_alt
д№Ӣй—ҙзҡ„е…ізі»гҖӮ - tiwopath
目录中的文件。 - Rob Cowie