Trong Phần 3 của loạt bài của chúng tôi trên mô -đun HĐH Python, chúng tôi sẽ chỉ cho bạn cách mở các tệp bằng mô -đun HĐH Python. Khởi chạy các tệp hoặc ứng dụng rất đơn giản với mô-đun HĐH Python nhờ các lệnh hệ thống tích hợp.
Nếu bạn bỏ lỡ nó, Phần 1 của loạt bài của chúng tôi trên mô -đun HĐH Python đã mô tả cách sử dụng CHDIR và GETCWD để đặt thư mục làm việc hiện tại của bạn. Phần 2 đã trình bày cách liệt kê tất cả các tệp và thư mục trong một thư mục bằng hệ điều hành Python.Part 1 of our series on the Python OS module described how to use chdir and getcwd to set your current working directory. Part 2 demonstrated how to list all files and folders in a directory using Python OS.
Hãy nhớ lại rằng trong loạt bài của chúng tôi trên mô -đun HĐH Python, chúng tôi sẽ giả sử cấu trúc thư mục sau:
Hướng dẫn đầu tiên của chúng tôi để lại với thư mục làm việc hiện tại của chúng tôi được đặt thành C: \ Thư mục chính bằng cách sử dụng tập lệnh sau:
import os
os.chdir[r"C:\main directory"]
wd = os.getcwd[]
Đây sẽ là điểm khởi đầu của chúng tôi cho hướng dẫn trong tuần này về việc mở các tệp, nhưng chúng tôi cũng sẽ hiển thị các ví dụ về cách mở các tệp, bất kể thư mục làm việc hiện tại của bạn.
Mở tập tin
Để mở một tệp bằng mô -đun HĐH Python, bạn cần nhập mô -đun HĐH, sau đó gọi phương thức system[]
và chuyển nó đường dẫn của tệp của bạn. Điều quan trọng là phải nhận ra rằng lệnh system[]
không mở một tệp phía sau hậu trường để đọc hoặc viết [tệp I/O]. Nó khởi chạy vật lý tệp hoặc ứng dụng của bạn trên màn hình của bạn bằng chương trình mặc định được liên kết với nó. Để mở các tệp để đọc và viết, chúng tôi có một hướng dẫn đầy đủ về tệp Python I/O.
Để kiểm tra điều này, tập lệnh sau sẽ mở tệp my_wordfile.docx trong thư mục làm việc hiện tại của bạn bằng Microsoft Word.
import os
os.system[r"my_wordfile.docx"]
Mặc dù tập lệnh này mặc định tìm kiếm một tệp trong thư mục làm việc hiện tại của bạn, phương thức system[]
thực sự cho phép bạn chỉ định một đường dẫn tệp đầy đủ. Để chứng minh điều này, hãy chạy tập lệnh Python sau, mở một tệp và sau đó in một lệnh cho bạn biết tệp đã được mở:
import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
Phần cuối cùng của loạt bài của chúng tôi trên mô -đun HĐH Python sẽ giải thích cách tạo và xóa các tệp và thư mục trên hệ thống của bạn. Hãy xem, và sau đó đăng ký bằng cách sử dụng biểu mẫu dưới đây để biết thêm các hướng dẫn miễn phí được thiết kế để biến bạn thành một chuyên gia Python thực sự.
Nhận Bộ phát triển Python của chúng tôi miễn phí
Tôi tập hợp một bộ phát triển Python với hơn 100 tập lệnh Python được xây dựng sẵn bao gồm các cấu trúc dữ liệu, gấu trúc, numpy, seeborn, học máy, xử lý tệp, quét web và nhiều hơn nữa - và tôi muốn bạn có nó miễn phí. Nhập địa chỉ email của bạn bên dưới và tôi sẽ gửi một bản sao theo cách của bạn.
Bài viết này được viết bởi Usman Malik, nhà văn đóng góp cho blog Python Tutorials.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
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.
Bàn luận
This method returns a file descriptor for newly open file. The returned file descriptor is non-inheritable.
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. os.open[path, flags, mode = 0o777, *, dir_fd = None]
Phương thức
os.open[]
trong Python được sử dụng để mở một đường dẫn tệp được chỉ định và đặt các cờ khác nhau theo các cờ được chỉ định và chế độ của nó theo chế độ được chỉ định. Phương thức này trả về một bộ mô tả tệp cho tệp mới mở. Bộ mô tả tệp được trả về là không thể không thể cưỡng lại được.
Path: A path-like object representing the file system path. This is the file path to be opened.
A path-like object is a string or bytes object which represents a path.
flags: This parameter specify the flags to be set for newly opened file.
mode [optional]: A numeric value representing the mode of the newly opened file. The default value of this parameter is 0o777 [octal].
dir_fd [optional]: A file descriptor referring to a directory.Cú pháp: os.open [đường dẫn, cờ, mode = 0o777, *, dir_fd = none] This method returns a file descriptor for newly opened file.
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. Đây là đường dẫn tệp được mở. Đối tượng giống như đường dẫn là một chuỗi hoặc đối tượng byte đại diện cho một đường dẫn. Chế độ của tệp mới được mở. Giá trị mặc định của tham số này là 0o777 [octal] .dir_fd [tùy chọn]: một mô tả tệp đề cập đến một thư mục. Use of os.open[]
method to open a file path
Loại trả về: Phương thức này trả về một bộ mô tả tệp cho tệp mới được mở.
Mã: Sử dụng phương thức os.open[]
để mở đường dẫn tệp
import
import os
os.system[r"my_wordfile.docx"]
0import os
os.system[r"my_wordfile.docx"]
1import os
os.system[r"my_wordfile.docx"]
2 import os
os.system[r"my_wordfile.docx"]
3import os
os.system[r"my_wordfile.docx"]
4import os
os.system[r"my_wordfile.docx"]
2 import os
os.system[r"my_wordfile.docx"]
6import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
5import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
6import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
7import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
8import os
os.system[r"my_wordfile.docx"]
7import os
os.system[r"my_wordfile.docx"]
2 import os
os.system[r"my_wordfile.docx"]
9File path opened successfully. String written to the file descriptor. String read from file descriptor: GeeksforGeeks: A computer science portal for geeks. File descriptor closed successfully.2
import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
9File path opened successfully. String written to the file descriptor. String read from file descriptor: GeeksforGeeks: A computer science portal for geeks. File descriptor closed successfully.4
import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
0import os
os.system[r"my_wordfile.docx"]
2 import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
2import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
3import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
4File path opened successfully. String written to the file descriptor. String read from file descriptor: GeeksforGeeks: A computer science portal for geeks. File descriptor closed successfully.9
system[]
0system[]
1system[]
0import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
8import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
9 import os
os.system[r"my_wordfile.docx"]
2 File path opened successfully. String written to the file descriptor. String read from file descriptor: GeeksforGeeks: A computer science portal for geeks. File descriptor closed successfully.1
import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
5import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
6system[]
9import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
8import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
5import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
6import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
9system[]
4system[]
5
import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
5import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
6system[]
8import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
8Output:
File path opened successfully. String written to the file descriptor. String read from file descriptor: GeeksforGeeks: A computer science portal for geeks. File descriptor closed successfully.
5import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
6import os
os.system[r"C:\Users\Public\Documents\SampleFile.txt"]
print["File opened"]
File path opened successfully.
String written to the file descriptor.
String read from file descriptor:
GeeksforGeeks: A computer science portal for geeks.
File descriptor closed successfully.
7File path opened successfully.
String written to the file descriptor.
String read from file descriptor:
GeeksforGeeks: A computer science portal for geeks.
File descriptor closed successfully.
8 //docs.python.org/3/library/os.html#os.open