共計 327 個字符,預計需要花費 1 分鐘才能閱讀完成。
要設置當前工作路徑,可以使用 os
模塊中的 chdir()
函數。
import os
# 獲取當前工作路徑
current_path = os.getcwd()
print(" 當前工作路徑:", current_path)
# 設置新的工作路徑
new_path = "/path/to/new/directory"
os.chdir(new_path)
# 檢查新的工作路徑
current_path = os.getcwd()
print(" 新的工作路徑:", current_path)
在上面的例子中,首先使用 os.getcwd()
函數獲取當前工作路徑,然后使用 os.chdir()
函數設置新的工作路徑。最后再次使用 os.getcwd()
函數來驗證設置是否成功。
丸趣 TV 網 – 提供最優質的資源集合!
正文完