Python os.chdir() Method
The os module in Python includes the chdir() function. The current working directory is used to utilize the default path for command execution, directory creation, and file creation. The current working directory is frequently used for commands and functions in command-line interfaces like bash, MS-DOS, and PowerShell. To change the current working directory, use the chdir() technique.
About os module:
When working with Python, there are many moments when we’d like to take advantage of the underlying operating system’s features. This is made possible by the python os module, which allows us to communicate with the underlying operating system in various ways and provides a portable mechanism to employ operating system-dependent functionality. Python’s built-in operating system dependant modules are designed so that they all utilize the same interface as long as the same functionality is accessible.
The os module also provides extensions specific to a given operating system, although utilizing them ultimately compromises portability. All functions that take path or file names accept both bytes and string objects as input, and the output is also an object of the same type if a path or file name is returned. When incorrect or unavailable file names and paths, or other parameters that have the right type but are not recognized by the operating system, all methods in the python os module raise the OSError (or subclasses thereof).
The os module allows you to use operating system-dependent functions on the go. If you only want to read or write a file, use open(), the os.path module if you want to change paths, and the file input module if you want to read all the lines in all the files on the command line. The tempfile module may be used to create temporary files and directories, while the shutil module can handle high-level file and directory operations.
Notes on these functionalities’ availability:
- All of Python’s built-in operating system dependant modules are designed such that they utilize the same interface as long as the same capability is available; for example, the function os.stat(path) provides stat information about the path in the same manner (which happens to have originated with the POSIX interface).
- The os module also provides extensions specific to a given operating system; however, utilizing them compromises portability.
- If a path or file name is supplied, all functions handling path or file names accept both bytes and string objects and return an object of the same kind.
- popen, os.fork, os.execv, and os.spawn*p* are not supported on VxWorks.
Code:
Output:
Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 1 The present working directory is / Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 2 >Enter the absolute path to the new directory to which we want to change:: level_1_dir Directory changed from / to level_1_dir. Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 1 The present working directory is /level_1_dir Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 3 The contents in the current directory are:: level_2_dir_1level_2_dir_2level_2_dir_3 Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 2 >Enter the absolute path to the new directory to which we want to change:: level_2_dir_1 Directory changed from /level_1_dir to level_2_dir_1. Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 1 The present working directory is /level_1_dir/level_2_dir_1 Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 3 The contents in the current directory are:: test_files_1_2_1_no4test_files_1_2_1_no9test_files_1_2_1_no8test_files_1_2_1_no1test_files_1_2_1_no7test_files_1_2_1_no6test_files_1_2_1_no10test_files_1_2_1_no2test_files_1_2_1_no3test_files_1_2_1_no5 Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 2 >Enter the absolute path to the new directory to which we want to change:: /level_1_dir/level_2_dir_3 Directory changed from /level_1_dir/level_2_dir_1 to /level_1_dir/level_2_dir_3. Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 1 The present working directory is /level_1_dir/level_2_dir_3 Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 3 The contents in the current directory are:: test_files_1_2_3_no7test_files_1_2_3_no8test_files_1_2_3_no3test_files_1_2_3_no5test_files_1_2_3_no10level_3_dirtest_files_1_2_3_no9test_files_1_2_3_no6test_files_1_2_3_no2test_files_1_2_3_no4test_files_1_2_3_no1 Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 2 >Enter the absolute path to the new directory to which we want to change:: level_3_dir Directory changed from /level_1_dir/level_2_dir_3 to level_3_dir. Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 1 The present working directory is /level_1_dir/level_2_dir_3/level_3_dir Do you want to continue or exit the code execution?[y/n] y Please choose one of the appropriate options:: 1. To check the current directory of the code execution. (using the getcwd() function) 2. To change the current directory of the code execution. (using the chdir() function) 3. To list the contents of the directory of the code execution. (using the listdir() function) 4. To exit from the code execution. 3 The contents in the current directory are:: test_file1test_file8test_file2test_file4test_file3test_file5test_file7test_file6test_file9test_file10 Do you want to continue or exit the code execution?[y/n] n
Explanation:
Now let’s look at the output of the above-written code. In the code written above, we have created different functions for the different functionalities related to the directories. This functionality is like printing the current working directory of the code execution and changing the current working directory to a new working directory for the code execution. The last and another functionality is to list all the contents in the current working directory. And in the last, a menu-driven code is written, which is used for providing the user all these three functionalities as options.
In the execution, we have executed the code in the root directory. After that, we have to change the current working directory of the code execution to the level one directory, which consists of three more directories which we have confirmed by opting for the third option, which prints all the contents of level 1 direct now our current working directory of the code execution is the level 1 direct and now we have opted the option to and change the current working directory to the level 2 directory 1. And then, we have listed the contents of this directory by selecting the options that list all the contents within the current working directory. We perform these steps a couple of times and end with the code execution by selecting the option when the code asks whether we want to continue the code execution or hold the code execution.
So in this article, we have seen how we can use the chdir() method provided by the OS module of python. We have also understood the various use case scenarios for users of this function and understood the actual implementation with the help of a sample code.