WindowsError:[错误2]系统找不到指定的文件:'tmp'

时间:2016-07-05 20:11:59

标签: python

import os

def create_temporary_directory(path, name):
    if not os.path.exists(os.path.join(path, name)):
        os.mkdir(os.path.join(path, name))

if __name__ == "__main__":
    name = 'tmp'
    create_temporary_directory('..', name)
    os.chdir(name)

print os.getcwd()

当我试图运行这个程序时,我错了“WindowsError:[错误2]系统找不到指定的文件:'tmp'”

2 个答案:

答案 0 :(得分:0)

os.chrdir(name)尝试访问'./tmp',但您在'../tmp'

创建了目录

只需更新您的代码:

if __name__ == "__main__":
    name = 'tmp'
    create_temporary_directory('..', name)
    os.chdir(os.path.join('..',name))

答案 1 :(得分:0)

以下是您可以这样做的方法:

import os

def create_temporary_directory(path, name):
    if not os.path.exists(os.path.join(path, name)):
        os.mkdir(os.path.join(path, name))
    return  os.path.join(path, name)

if __name__ == "__main__":
    name = 'tmp'
    path = create_temporary_directory('..', name)
    os.chdir(path)
    print os.getcwd()