📜  Python os.chdir() 方法

📅  最后修改于: 2022-05-13 01:55:21.222000             🧑  作者: Mango

Python os.chdir() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统,属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.chdir()方法用于将当前工作目录更改为指定路径。它只需要一个参数作为新目录路径。

代码 #1:使用 chdir() 更改目录

Python3
# Python3 program to change the
# directory of file using os.chdir() method
 
# import os library
import os
 
# change the current directory
# to specified directory
os.chdir(r"C:\Users\Gfg\Desktop\geeks")
 
print("Directory changed")


Python3
# import os module
import os
 
# change the current working directory
# to specified path
os.chdir('c:\\gfg_dir')
 
# verify the path using getcwd()
cwd = os.getcwd()
 
# print the current directory
print("Current working directory is:", cwd)


Python3
# importing all necessary libraries
import sys, os
 
# initial directory
cwd = os.getcwd()
 
# some non existing directory
fd = 'false_dir / temp'
 
# trying to insert to false directory
try:
    os.chdir(fd)
    print("Inserting inside-", os.getcwd())
     
# Caching the exception   
except:
    print("Something wrong with specified\
          directory. Exception- ", sys.exc_info())
           
# handling with finally         
finally:
    print("Restoring the path")
    os.chdir(cwd)
    print("Current directory is-", os.getcwd())


输出:

Directory changed

代码 #2:使用 os.getcwd()
要知道文件的当前工作目录,可以使用 getcwd() 方法。更改路径后,可以使用此方法验证当前工作目录的路径。

Python3

# import os module
import os
 
# change the current working directory
# to specified path
os.chdir('c:\\gfg_dir')
 
# verify the path using getcwd()
cwd = os.getcwd()
 
# print the current directory
print("Current working directory is:", cwd)

输出:

Current working directory is: c:\\gfg_dir


代码 #3:在更改目录时处理错误

Python3

# importing all necessary libraries
import sys, os
 
# initial directory
cwd = os.getcwd()
 
# some non existing directory
fd = 'false_dir / temp'
 
# trying to insert to false directory
try:
    os.chdir(fd)
    print("Inserting inside-", os.getcwd())
     
# Caching the exception   
except:
    print("Something wrong with specified\
          directory. Exception- ", sys.exc_info())
           
# handling with finally         
finally:
    print("Restoring the path")
    os.chdir(cwd)
    print("Current directory is-", os.getcwd())

输出:

Inserting inside- c:\gfg_dir\gfg
Something wrong with specified directory. Exception- 
Restoring the path
Current directory is- c:\gfg_dir\gfg