WindowsError: [Error 2] The system cannot find the file specified: 'tmp'

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()

when i tried to run this program, i'm error "WindowsError: [Error 2] The system cannot find the file specified: 'tmp'"

Upvotes: 0

Views: 646

Answers (2)

dmitryro
dmitryro

Reputation: 3516

Here's how you can do this:

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()

Upvotes: 0

Xander
Xander

Reputation: 96

os.chrdir(name) tries to reach './tmp', but you created your directory at '../tmp'

just update your code that way :

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

Upvotes: 0

Related Questions