Bạn có thể liệt kê tất cả các tệp trong thư mục hiện tại bằng
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
7:
import os
for filename in os.listdir(os.getcwd()):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
Quả cầu
Hoặc bạn chỉ có thể liệt kê một số tệp, tùy thuộc vào mẫu tệp bằng mô -đun
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
8:
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
Nó không phải là thư mục hiện tại mà bạn có thể liệt kê chúng trong bất kỳ đường dẫn nào bạn muốn:
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
Đường ống
Hoặc thậm chí bạn có thể sử dụng đường ống như bạn đã chỉ định bằng cách sử dụng
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
9
import fileinput
for line in fileinput.input():
# do your stuff
Và sau đó bạn có thể sử dụng nó với đường ống:
ls -1 | python parse.py
Có nhiều mô-đun và phương pháp tích hợp để xử lý tệp trong Python. Một số chức năng này có thể được tìm thấy trong HĐH, GLOB và nhiều mô -đun hơn.
Các mô -đun HĐH khác nhau trong lập trình Python cho phép nhiều phương thức tương tác với hệ thống tệp. Nó có chức năng
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
5 cho phép chúng tôi liệt kê tất cả các tệp trong một đường dẫn cụ thể bằng cách đi qua thư mục từ dưới lên hoặc từ trên xuống và trả về ba bộ dữ liệu-root, dir và tệp.
Cú pháp:
Có nhiều mô-đun và phương pháp tích hợp để xử lý tệp trong Python. Một số chức năng này có thể được tìm thấy trong HĐH, GLOB và nhiều mô -đun hơn.
Các mô -đun HĐH khác nhau trong lập trình Python cho phép nhiều phương thức tương tác với hệ thống tệp. Nó có chức năng
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
5 cho phép chúng tôi liệt kê tất cả các tệp trong một đường dẫn cụ thể bằng cách đi qua thư mục từ dưới lên hoặc từ trên xuống và trả về ba bộ dữ liệu-root, dir và tệp.
Cú pháp:
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
Output:
This is the first file.
This is the second file.
This is the last file.
Trong cú pháp trên,
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
6 là đọc thư mục gốc hoặc thư mục và tham số
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
7 là đường dẫn của thư mục.
Các mô -đun HĐH khác nhau trong lập trình Python cho phép nhiều phương thức tương tác với hệ thống tệp. Nó có chức năng import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
5 cho phép chúng tôi liệt kê tất cả các tệp trong một đường dẫn cụ thể bằng cách đi qua thư mục từ dưới lên hoặc từ trên xuống và trả về ba bộ dữ liệu-root, dir và tệp.
Mô -đun
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
8 được sử dụng để liệt kê các tệp bên trong một thư mục cụ thể. Hàm
import fileinput
for line in fileinput.input():
# do your stuff
9 bên trong mô -đun
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
8 được sử dụng để có một danh sách các tệp hoặc thư mục con phù hợp với một mẫu được chỉ định bên trong một thư mục được chỉ định. Hàm
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
1 lấy mẫu làm tham số đầu vào và trả về danh sách các tệp và thư mục con bên trong thư mục được chỉ định.
Chúng ta có thể lặp lại tất cả các tệp văn bản bên trong một thư mục cụ thể bằng cách sử dụng hàm
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
1 và mở chúng với hàm
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
8 trong Python. Ví dụ mã sau đây cho chúng ta biết cách chúng ta có thể mở tất cả các tệp trong một thư mục với các chức năng
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
1and
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
8:
import glob
import os
for filename in glob.glob('files\*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
text = f.read()
print(text)
Output:
This is the first file.
This is the second file.
This is the last file.
Chúng tôi đọc văn bản từ ba tệp bên trong thư mục
import fileinput
for line in fileinput.input():
# do your stuff
1 và in nó trên thiết bị đầu cuối trong mã trên. Đầu tiên chúng tôi đã sử dụng vòng lặp
import fileinput
for line in fileinput.input():
# do your stuff
2 với hàm
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
1 để lặp qua mỗi tệp được tìm thấy bên trong thư mục
import fileinput
for line in fileinput.input():
# do your stuff
4. Sau đó, chúng tôi đã mở từng tệp ở chế độ
import fileinput
for line in fileinput.input():
# do your stuff
5 với hàm
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
8 và in văn bản bên trong mỗi tệp.
Mở tất cả các tệp trong một thư mục bằng cách sử dụng
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
2 trong Python
Mở tất cả các tệp trong một thư mục bằng cách sử dụng
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
0 trong Python
Sự kết luận
Bài viết này đã thảo luận về các phương pháp khác nhau để mở tất cả các tệp trong một thư mục có phần mở rộng. Các phương pháp chúng tôi đã sử dụng là các phương pháp
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
5 và
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
5 của mô -đun HĐH, cũng như phương pháp
import fileinput
for line in fileinput.input():
# do your stuff
9 của mô -đun GLOB.
Mở tất cả các tệp trong một thư mục bằng cách sử dụng
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
2 trong Python
Mở tất cả các tệp trong một thư mục bằng cách sử dụng import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
2 trong Python
Mở tất cả các tệp trong một thư mục bằng cách sử dụng
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
0 trong Python
Một thư mục, còn được gọi là thư mục, là một cấu trúc tổ chức đơn vị trong hệ thống tệp máy tính được sử dụng để lưu trữ và xác định vị trí các tệp hoặc thư mục. Python có một số API để liệt kê nội dung của một thư mục.
os.walk(r'pathname')
Có nhiều mô-đun và phương pháp tích hợp để xử lý tệp trong Python. Một số chức năng này có thể được tìm thấy trong HĐH, GLOB và nhiều mô -đun hơn.
Example:
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
0
Các mô -đun HĐH khác nhau trong lập trình Python cho phép nhiều phương thức tương tác với hệ thống tệp. Nó có chức năng
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
5 cho phép chúng tôi liệt kê tất cả các tệp trong một đường dẫn cụ thể bằng cách đi qua thư mục từ dưới lên hoặc từ trên xuống và trả về ba bộ dữ liệu-root, dir và tệp.
Cú pháp:
Output:
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
1
Trong cú pháp trên,
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
6 là đọc thư mục gốc hoặc thư mục và tham số
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
7 là đường dẫn của thư mục.
Mở tất cả các tệp trong một thư mục bằng cách sử dụng import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
0 trong Python
Một thư mục, còn được gọi là thư mục, là một cấu trúc tổ chức đơn vị trong hệ thống tệp máy tính được sử dụng để lưu trữ và xác định vị trí các tệp hoặc thư mục. Python có một số API để liệt kê nội dung của một thư mục.
Có nhiều mô-đun và phương pháp tích hợp để xử lý tệp trong Python. Một số chức năng này có thể được tìm thấy trong HĐH, GLOB và nhiều mô -đun hơn.
Syntax:
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
2
Có nhiều mô-đun và phương pháp tích hợp để xử lý tệp trong Python. Một số chức năng này có thể được tìm thấy trong HĐH, GLOB và nhiều mô -đun hơn.
Example:
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
3
Các mô -đun HĐH khác nhau trong lập trình Python cho phép nhiều phương thức tương tác với hệ thống tệp. Nó có chức năng
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
5 cho phép chúng tôi liệt kê tất cả các tệp trong một đường dẫn cụ thể bằng cách đi qua thư mục từ dưới lên hoặc từ trên xuống và trả về ba bộ dữ liệu-root, dir và tệp.
Output:
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
4
Mở tất cả các tệp trong một thư mục bằng cách sử dụng import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
1 trong Python
Trong các ví dụ trước, chúng tôi đã phải lặp lại một danh sách các tệp trong một thư mục có tên khớp với các tiện ích mở rộng hoặc mẫu cụ thể. Tuy nhiên, các mô -đun GLOB cho phép bạn tìm một danh sách các tệp có các tiện ích mở rộng hoặc mẫu cụ thể.
Syntax:
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
5
Có hai tham số cho chức năng này.
Đầu tiên là
This is the first file.
This is the second file.
This is the last file.
8 với một mẫu cụ thể, trong đó lọc tất cả các tệp và trả về danh sách.
Một tham số khác được gọi là
This is the first file.
This is the second file.
This is the last file.
9 được đặt thành
import glob
import os
for filename in glob.glob('files\*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
text = f.read()
print(text)
0 theo mặc định, có nghĩa là
import glob
import os
for filename in glob.glob('files\*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
text = f.read()
print(text)
1. Khi giá trị của nó là
import glob
import os
for filename in glob.glob('files\*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
text = f.read()
print(text)
2, hàm tìm kiếm thư mục và thư mục con của nó.
Tất cả các thẻ hoang dã, chẳng hạn như
import glob
import os
for filename in glob.glob('files\*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
text = f.read()
print(text)
3,
import glob
import os
for filename in glob.glob('files\*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
text = f.read()
print(text)
4, v.v., được phép ở đây.
Example:
import os, glob
for filename in glob.glob('*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
6
Chúng tôi đã nhập hai mô -đun Python trong mã trên - Glob và OS. Chúng tôi sử dụng chức năng
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
8 và chức năng
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
1 để mở tất cả các tệp với tiện ích mở rộng
import glob
import os
for filename in glob.glob('files\*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
text = f.read()
print(text)
7 trong các tệp
Sự kết luận
Bài viết này đã thảo luận về các phương pháp khác nhau để mở tất cả các tệp trong một thư mục có phần mở rộng. Các phương pháp chúng tôi đã sử dụng là các phương pháp
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
5 và
import os, glob
path = '/some/path/to/file'
for filename in glob.glob(os.path.join(path, '*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r') as f: # open in readonly mode
# do your stuff
5 của mô -đun HĐH, cũng như phương pháp
import fileinput
for line in fileinput.input():
# do your stuff