Hướng dẫn dirname python - con trăn dirname

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.path module is sub module of OS module in Python used for common path name manipulation.

    Bàn luận

    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ệ điều hành 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. Mô -đun OS.Path là mô -đun phụ của mô -đun HĐH trong Python được sử dụng để thao tác tên đường dẫn phổ biến. os.path.dirname(path)

    Phương thức

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    5 trong Python được sử dụng để lấy tên thư mục từ đường dẫn được chỉ định.
    path: A path-like object representing a file system path.

    Cú pháp: OS.Path.Dirname (đường dẫn) This method returns a string value which represents the directory name from the specified path.

    Tham số: Đường dẫn: Một đối tượng giống như đường dẫn biểu thị đường dẫn hệ thống tệp. Use of

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    5 method

    Loại trả về: Phương thức này trả về một giá trị chuỗi đại diện cho tên thư mục từ đường dẫn được chỉ định.

    Mã: Sử dụng phương thức

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    5

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    7
      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    8

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    5
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    6

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    9
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    0
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    1

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    7
      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    8

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    5
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    6

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    9
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    0
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    1

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    7
      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    8

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    5
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    6

    Output:

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    9
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    0
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    1

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    2
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    0
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    4
    https://docs.python.org/3/library/os.path.html

    Hướng dẫn cách lấy tên file, thư mục và đuôi file trong python. Bạn sẽ học được cách sử dụng các hàm trong module os như os.path.basename(), os.path.dirname(), os.path.split(), os.path.splitext() trong python để lấy tên file, tên thư mục, đường dẫn thư mục và đuôi file trong python sau bài học này.lấy tên file, thư mục và đuôi file trong python. Bạn sẽ học được cách sử dụng các hàm trong module os như os.path.basename(), os.path.dirname(), os.path.split(), os.path.splitext() trong python để lấy tên file, tên thư mục, đường dẫn thư mục và đuôi file trong python sau bài học này.

    Lấy tên file trong python | os.path.basename()

    Để lấy tên file trong python, chúng ta sử dụng hàm os.path.basename() trong module os, với cú pháp sau đây:

    os.path.basename(path)

    Trong đó

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    3 là đường dẫn của file cần lấy tên trong python. Kết quả chương trình, một tên file đầy đủ bao gồm cả tên và đuôi file sẽ được trả về.

    Ví dụ, chúng ta có một thư mục như sau:

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    

    Chúng ta lấy tên của file

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    4 như sau:

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    print(type(basename))

    Lưu ý, chúng ta có thể lấy tên file trong python ngay cả khi file đó vốn không tồn tại bằn hàm os.path.basename(). Do hàm này không kiểm tra file có tồn tại hay không, nên lỗi không xảy ra:

    import os
    filepath_not_exist = './dir1/address.txt'

    basename = os.path.basename(filepath_not_exist)
    print(basename)

    Nếu bạn chỉ muốn lấy tên file trong python với những file đã tồn tại, hãy sử dụng kết hợp với phương pháp kiểm tra file có tồn tại hay không mà Kiyoshi đã giới thiệu tại bài viết Kiểm tra tồn tại của file và thư mục trong python.

    Lấy đuôi file trong python | os.path.splitext()

    Để Lấy tên file trong python, chúng ta sử dụng hàm os.path.basename() ở trên kết hợp với hàm os.path.splitext() trong module os, với cú pháp sau đây:

    os.path.splitext(filename)

    Trong đó

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    5 là tên file đầy đủ bao gồm cả tên và đuôi file của file cần lấy đuôi file trong python.Hàm os.path.splitext() sẽ chia
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    6 thành hai phần tại vị trí dấu chấm
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    7 cuối cùng ở bên phải, và lưu giữ kết quả dưới dạng một tuple.
    Hàm os.path.splitext() sẽ chia
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    6 thành hai phần tại vị trí dấu chấm
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    7 cuối cùng ở bên phải, và lưu giữ kết quả dưới dạng một tuple.

    Thông thường, file sẽ có dạng

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    8 +
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    7 +
    import os
    filepath = './dir1/user.txt'

    file_name, file_extension = os.path.splitext(os.path.basename(filepath))
    print(file_name)


    print(file_extension)

    0, do đó sau khi dùng hàm os.path.splitext() thì tên file và đuôi file( bao gồm cả dấu chấm
    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    7) sẽ được tách ra và lưu giữ dưới dạng một tuple.Tùy vào mục đích sử dụng, ví dụ như chúng ta muốn lấy đuôi file trong python hay là lấy tên file không gồm đuôi file trong python mà chúng ta lấy thông tin ra từ tuple tạo ra.
    Tùy vào mục đích sử dụng, ví dụ như chúng ta muốn lấy đuôi file trong python hay là lấy tên file không gồm đuôi file trong python mà chúng ta lấy thông tin ra từ tuple tạo ra.

    Ví dụ, chúng ta lấy đuôi file và tên file không gồm đuôi file trong python như sau:

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    Nngoài cách truy cập vào phần tử trong tuple và lấy ra đuôi file , tên như ở trên, chúng ta còn có thể giải nén tuple và gán biến rồi sử dụng trực tiếp như dưới đây.

    import os
    filepath = './dir1/user.txt'

    file_name, file_extension = os.path.splitext(os.path.basename(filepath))
    print(file_name)


    print(file_extension)

    Lưu ý, do trong kết quả của hàm os.path.splitext() sẽ bao gồm cả dấu chấm

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    7 ở trong đuôi file, do đó nếu bạn muốn bỏ đi dấu chấm này, hãy sử dụng kết hợp với hàm tách chuỗi trong python, hoặc là dùng cách xóa đi dấu chấm này bằng cách dùng hàm xóa ký tự trong chuỗi python.

    file_extension_without_dot=name_tuple[1].strip(".")

    Lại nữa, do hàm os.path.splitext() chỉ tách đối số được chỉ định thành hai phần tại vị trí dấu chấm cuối cùng bên phải, nên với các file mà tên file được tạo ra từ nhiều hơn hai dấu chấm như

    import os
    filepath = './dir1/user.txt'

    file_name, file_extension = os.path.splitext(os.path.basename(filepath))
    print(file_name)


    print(file_extension)

    3, chúng ta cần phải sử dụng hàm này hai lần để có thể lấy tên file không kèm đuôi file trong python.lấy tên file không kèm đuôi file trong python.

    import os
    filepath_tar_gz = './data/dir1/filename.tar.gz'
    basename=os.path.basename(filepath_tar_gz)
    print(basename)


    basename_split1=os.path.splitext(basename)
    print(basename_split1[0])


    basename_split2=os.path.splitext(basename_split1)
    print(basename_split2[0])

    Lấy tên thư mục trong python | os.path.basename()

    Chúng ta cũng dùng hàm os.path.basename() để lấy tên thư mục trong python, bằng cách chỉ định đối số

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    3 là một đường dẫn thư mục

    os.path.basename(path)

    Cách sử dụng cũng tương tự như khi lấy tên file trong python, ví dụ:

    import os

    folderpath = './user/dir1'
    print(os.path.basename(folderpath))

    Lấy đường dẫn thư mục trong python | os.path.dirname()

    Để lấy đường dẫn thư mục trong python, chúng ta sử dụng hàm os.path.dirname() trong module os, với cú pháp sau đây:

    os.path.dirname(path)

    Trong đó

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    3 là đường dẫn của file hoặc thư mục cần lấy đường dẫn thư mục mẹ. Sau khi chạy, đường dẫn của thư mục mẹ chứa file hoặc thư mục chỉ đinh sẽ được trả về.Ví dụ:
    Ví dụ:

    import os
    filepath = './user/dir1/user.txt'
    print(os.path.dirname(filepath))


    dir_path='./user/dir1'
    print(os.path.dirname(dir_path))

    Tương tự như với hàm os.path.basename() thì hàm os.path.dirname() không kiểm tra sự tồn tại của đường dẫn chỉ định, do đó chúng ta cũng có thể lấy đường dẫn của thư mục kể cả thư mục đó vốn không tồn tai trước đó:

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    0

    Trong trường hợp chúng ta cần lấy tên của thư mục mẹ chứa file hoặc thư mục đã được chỉ định, hãy sử dụng kèm với hàm os.path.basename() như sau

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    1

    Lấy tên file kèm tên thư mục trong python | os.path.split()

    Để lấy tên file kèm tên thư mục chứa nó trong python, chúng ta sử dụng hàm os.path.split() trong module os với cú pháp sau đây:

    os.path.split(path)

    Trong đó

    import os
    filepath = './dir1/user.txt'

    basename = os.path.basename(filepath)
    print(basename)


    name_tuple= os.path.splitext(basename)
    print(name_tuple)


    filename=name_tuple[0]
    print(filename)


    file_extension=name_tuple[1]
    print(file_extension)

    3 là đường dẫn của file cần lấy tên file kèm tên thư mục chứa nó.Hàm os.path.split() sẽ tách đường dẫn chỉ định thành hai phần, tại vị trí dấu phân cách đường dẫn cuối cùng bên phải, sau đó lưu giữ kết quả vào một tuple.
    Hàm os.path.split() sẽ tách đường dẫn chỉ định thành hai phần, tại vị trí dấu phân cách đường dẫn cuối cùng bên phải, sau đó lưu giữ kết quả vào một tuple.

    Ví dụ:

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    2

    Để lấy tên file và tên thư mục chứa file đó, chúng ta có thể truy cập vào phần tử trong tuple ở trên như sau:

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    3

    Ngoài ra, chúng ta có thể giải nén tuple trên và gán biến rồi sử dụng như dưới đây.

      |data/
      |--dir1
      |  |
      |  |--user.txt
      |
      |--dir2
      |
      |--take_file_name.py
    
    4

    Tổng kết và thực hành

    Trên đây Kiyoshi đã hướng dẫn bạn về cách lấy tên file, thư mục và đuôi file trong python rồi. Để nắm rõ nội dung bài học hơn, bạn hãy thực hành viết lại các ví dụ của ngày hôm nay nhé.lấy tên file, thư mục và đuôi file trong python rồi. Để nắm rõ nội dung bài học hơn, bạn hãy thực hành viết lại các ví dụ của ngày hôm nay nhé.

    Và hãy cùng tìm hiểu những kiến thức sâu hơn về python trong các bài học tiếp theo.

    Viết bởi Kiyoshi. Đã đăng ký bản quyền tác giả tại Creativecommons và DMCA