with open('/tests/testDict.pickle','wb') as fp: print 'to terminal'
上面的内容有什么问题让我发疯并给了我以下内容:
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-17-fe5a4a9f361c> in <module>()
----> 1 with open('/tests/testDict.pickle','wb') as fp: print 'to terminal'
IOError: [Errno 2] No such file or directory: '/tests/testDict.pickle'
如果前面没有斜杠,我会得到:
with open('tests/testDict.pickle','wb') as fp: print 'to terminal'
to terminal
我在 OS X 中有以下目录结构:
$ ls
cp1.py cp1_pdftest.jpg random/ tests/
请您参考如下方法:
"/tests/" 上的前导斜杠强制路径为绝对路径,即使您正在这样做也是如此
os.path.join(os.getcwd(), "/tests/")
所以你需要做
os.path.join(os.getcwd(), "tests/")
来自os.path.join文档:
os.path.join(path, *paths)Join one or more path components intelligently. The return value is the concatenation of
pathand any members of*pathswith exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
