Python os.chdir() 方法2024 年 8 月 29 日 | 阅读 11 分钟 Python 的 os 模块包含 chdir() 函数。当前工作目录用于利用默认路径来执行命令、创建目录和创建文件。当前工作目录经常用于 bash、MS-DOS 和 PowerShell 等命令行界面中的命令和函数。要更改当前工作目录,请使用 chdir() 方法。 关于 os 模块在处理 Python 时,有很多时候我们希望利用底层操作系统的特性。这可以通过 Python 的 os 模块来实现,该模块允许我们以多种方式与底层操作系统进行通信,并提供了一种可移植的机制来运用操作系统依赖的功能。Python 内置的操作系统依赖模块设计得当,只要相同的功能可用,它们都会使用相同的接口。 os 模块还提供了特定于给定操作系统的扩展,尽管使用它们最终会影响可移植性。所有接受路径或文件名的函数都接受字节和字符串对象作为输入,如果返回路径或文件名,输出也是相同类型的对象。当文件名称和路径不正确或不可用,或者其他类型正确但未被操作系统识别的参数出现时,Python os 模块中的所有方法都会引发 OSError(或其子类)。 os 模块允许您在移动中(on the go)使用操作系统依赖的功能。如果您只想读取或写入文件,请使用 open();如果您想更改路径,请使用 os.path 模块;如果您想读取命令行中所有文件的所有行,请使用文件输入模块。tempfile 模块可用于创建临时文件和目录,而 shutil 模块可以处理高级文件和目录操作。 关于这些功能可用性的注意事项
代码输出 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_1 level_2_dir_2 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. 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_no4 test_files_1_2_1_no9 test_files_1_2_1_no8 test_files_1_2_1_no1 test_files_1_2_1_no7 test_files_1_2_1_no6 test_files_1_2_1_no10 test_files_1_2_1_no2 test_files_1_2_1_no3 test_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_no7 test_files_1_2_3_no8 test_files_1_2_3_no3 test_files_1_2_3_no5 test_files_1_2_3_no10 level_3_dir test_files_1_2_3_no9 test_files_1_2_3_no6 test_files_1_2_3_no2 test_files_1_2_3_no4 test_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_file1 test_file8 test_file2 test_file4 test_file3 test_file5 test_file7 test_file6 test_file9 test_file10 Do you want to continue or exit the code execution?[y/n] n 说明 现在我们来看上面代码的输出。在上面编写的代码中,我们为与目录相关的不同功能创建了不同的函数。这些功能包括打印代码执行的当前工作目录,以及将代码执行的当前工作目录更改为新的工作目录。最后,另一项功能是列出当前工作目录中的所有内容。最后,编写了一个菜单驱动的代码,用于向用户提供所有这三个功能作为选项。 在执行中,我们在根目录下执行了代码。之后,我们需要将代码执行的当前工作目录更改为一级目录,其中包含三个子目录,我们通过选择打印一级目录所有内容的第三个选项来确认这一点。现在代码执行的当前工作目录是一级目录,现在我们选择了选项二,将代码执行的当前工作目录更改为二级目录 1。然后,我们通过选择列出当前工作目录中所有内容的选项来列出该目录的内容。我们执行这些步骤几次,并通过选择代码询问是否要继续执行代码或暂停执行代码的选项来结束代码执行。 所以在这篇文章中,我们已经看到了如何使用 Python 的 OS 模块提供的 chdir() 方法。我们还通过示例代码理解了此函数的各种用例场景和实际实现。 下一话题Python 中的括号匹配 |
我们请求您订阅我们的新闻通讯以获取最新更新。