Hướng dẫn how do i change a path in python? - làm cách nào để thay đổi đường dẫn trong python?

Trong bài viết này, chúng tôi sẽ thảo luận về cách thay đổi thư mục làm việc hiện tại trong Python.

Thư mục làm việc hiện tại là thư mục trong đó chương trình đang chạy.

Trước hết chúng ta cần nhập mô -đun HĐH Python, tức là.

import os

Mô -đun HĐH Python cung cấp một chức năng để thay đổi thư mục làm việc hiện tại, tức là.os module provides a function to change the current working directory i.e.

os.chdir(path)

Nó thay đổi thư mục làm việc hiện tại thành đường dẫn đã cho.

Hãy để hiểu bằng một ví dụ,

Quảng cáo

Đầu tiên in thư mục làm việc hiện tại bằng os.getcwd () tức là.

print("Current Working Directory " , os.getcwd())

Bây giờ, hãy để thay đổi thư mục làm việc hiện tại bằng cách sử dụng os.chdir (đường dẫn), tức là.

os.chdir("/home/varun/temp")

Nếu đường dẫn đã cho don lồng tồn tại thì Os.Chdir () có lỗi ném: FilenotFounderror. & Nbsp; do đó chúng ta nên gọi nó bằng cách sử dụng thử / ngoại trừ tức là.FileNotFoundError. Therefore we should either call it using try / except i.e.

try:
    # Change the current working Directory    
    os.chdir("/home/varun/temp")
    print("Directory changed")
except OSError:
    print("Can't change the Current Working Directory")        

hoặc kiểm tra xem thư mục mới có tồn tại trước khi thay đổi thư mục làm việc, tức là không.

# Check if New path exists
if os.path.exists("/home/varun/temp") :
    # Change the current working Directory    
    os.chdir("/home/varun/temp")
else:
    print("Can't change the Current Working Directory")    

Ví dụ hoàn chỉnh như sau,

import os

def main():
    
    print("Current Working Directory " , os.getcwd())
    
    
    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        

    print("Current Working Directory " , os.getcwd())
    
    # Check if New path exists
    if os.path.exists("/home/varun/temp") :
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
    else:
        print("Can't change the Current Working Directory")    

        
    
    print("Current Working Directory " , os.getcwd())
    
if __name__ == '__main__':
    main()

Output:

Current Working Directory  /home/varun/Documents/blog/pythonSamples/FileSamples
Directory changed
Current Working Directory  /home/varun/temp
Current Working Directory  /home/varun/temp
& nbsp;
 

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc in Python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. 
    os.chdir() method in Python used to change the current working directory to specified path. It takes only a single argument as new directory path.
     

    Bàn luận os.chdir(path)
    Parameters: 
    path: A complete path of directory to be changed to new directory path.
    Returns: Doesn’t return any value

    Mô -đun HĐH trong Python cung cấp các chức năng để tương tác với hệ điều hành. HĐH, thuộc các mô -đun tiện ích tiêu chuẩn Python. Mô -đun này cung cấp một cách di động để sử dụng chức năng phụ thuộc hệ điều hành. Nó chỉ lấy một đối số duy nhất làm đường dẫn thư mục mới. & Nbsp; Use chdir() to change the directory

    Python3

    Cú pháp: Os.Chdir (Path) Tham số: & nbsp; đường dẫn: Đường dẫn hoàn chỉnh của thư mục sẽ được thay đổi thành đường dẫn thư mục mới. Returns: không trả về bất kỳ giá trị nào

    os.chdir(path)
    3
    os.chdir(path)
    4
    os.chdir(path)
    5

    os.chdir(path)
    6
    os.chdir(path)
    7
    os.chdir(path)
    8
    os.chdir(path)
    5

    Output:    

    Directory changed

    Mã số 1: Sử dụng chdir () để thay đổi thư mục Use of os.getcwd() 
    To know the current working directory of the file, getcwd() method can be used. After changing the path, one can verify the path of current working directory using this method. 

    Python3

    Cú pháp: Os.Chdir (Path) Tham số: & nbsp; đường dẫn: Đường dẫn hoàn chỉnh của thư mục sẽ được thay đổi thành đường dẫn thư mục mới. Returns: không trả về bất kỳ giá trị nào

    print("Current Working Directory " , os.getcwd())
    2
    print("Current Working Directory " , os.getcwd())
    3
    os.chdir(path)
    5

    Mã số 1: Sử dụng chdir () để thay đổi thư mục

    os.chdir(path)
    6
    os.chdir(path)
    7
    os.chdir("/home/varun/temp")
    0
    os.chdir("/home/varun/temp")
    1

    Output:    

    Current working directory is: c:\\gfg_dir

    os.chdir(path)
    1
    os.chdir(path)
    2
    Code #3: Handling the errors while changing the directory 

    Python3

    Mã số 2: Sử dụng OS.GetCwd () & NBSP; Để biết thư mục làm việc hiện tại của tệp, phương thức getCwd () có thể được sử dụng. Sau khi thay đổi đường dẫn, người ta có thể xác minh đường dẫn của thư mục làm việc hiện tại bằng phương pháp này. & NBSP;

    print("Current Working Directory " , os.getcwd())
    5
    print("Current Working Directory " , os.getcwd())
    6
    print("Current Working Directory " , os.getcwd())
    7

    & nbsp; & nbsp; Mã số 3: Xử lý các lỗi trong khi thay đổi thư mục & nbsp;

    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    0
    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    1

    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    2
    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    3

    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    2
    os.chdir(path)
    6
    os.chdir(path)
    7
    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    7
    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    8

    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    9
    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    1

    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    2
    os.chdir(path)
    6
    # Check if New path exists
    if os.path.exists("/home/varun/temp") :
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
    else:
        print("Can't change the Current Working Directory")    
    
    3

    os.chdir(path)
    1
    os.chdir("/home/varun/temp")
    3

    # Check if New path exists
    if os.path.exists("/home/varun/temp") :
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
    else:
        print("Can't change the Current Working Directory")    
    
    8
    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    1

    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    2
    os.chdir(path)
    6
    os.chdir(path)
    7
    import os
    
    def main():
        
        print("Current Working Directory " , os.getcwd())
        
        
        try:
            # Change the current working Directory    
            os.chdir("/home/varun/temp")
            print("Directory changed")
        except OSError:
            print("Can't change the Current Working Directory")        
    
        print("Current Working Directory " , os.getcwd())
        
        # Check if New path exists
        if os.path.exists("/home/varun/temp") :
            # Change the current working Directory    
            os.chdir("/home/varun/temp")
        else:
            print("Can't change the Current Working Directory")    
    
            
        
        print("Current Working Directory " , os.getcwd())
        
    if __name__ == '__main__':
        main()
    
    
    3
    os.chdir(path)
    5

    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    2
    import os
    
    def main():
        
        print("Current Working Directory " , os.getcwd())
        
        
        try:
            # Change the current working Directory    
            os.chdir("/home/varun/temp")
            print("Directory changed")
        except OSError:
            print("Can't change the Current Working Directory")        
    
        print("Current Working Directory " , os.getcwd())
        
        # Check if New path exists
        if os.path.exists("/home/varun/temp") :
            # Change the current working Directory    
            os.chdir("/home/varun/temp")
        else:
            print("Can't change the Current Working Directory")    
    
            
        
        print("Current Working Directory " , os.getcwd())
        
    if __name__ == '__main__':
        main()
    
    
    6

    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    2
    os.chdir(path)
    6
    os.chdir(path)
    7
    Current Working Directory  /home/varun/Documents/blog/pythonSamples/FileSamples
    Directory changed
    Current Working Directory  /home/varun/temp
    Current Working Directory  /home/varun/temp
    
    0
    try:
        # Change the current working Directory    
        os.chdir("/home/varun/temp")
        print("Directory changed")
    except OSError:
        print("Can't change the Current Working Directory")        
    
    8

    Output:  

    os.chdir(path)
    0

    Làm cách nào để thay đổi đường dẫn của một tệp trong Python?

    Phương thức CHDIR () trong Python được sử dụng để thay đổi thư mục làm việc hiện tại thành đường dẫn được chỉ định.Nó chỉ lấy một đối số duy nhất làm đường dẫn thư mục mới. in Python used to change the current working directory to specified path. It takes only a single argument as new directory path.

    Làm cách nào để thay đổi đường dẫn của một thư mục trong Python?

    Để thay đổi phương thức thư mục làm việc hiện tại (CWD) OS.Chdir () được sử dụng.Phương pháp này thay đổi CWD thành một đường dẫn được chỉ định.Nó chỉ lấy một đối số duy nhất làm đường dẫn thư mục mới.os. chdir() method is used. This method changes the CWD to a specified path. It only takes a single argument as a new directory path.