2012-04-30 54 views

Trả lời

324

Original câu trả lời:

for filename in os.listdir(directory): 
    if filename.endswith(".asm") or filename.endswith(".py"): 
     # print(os.path.join(directory, filename)) 
     continue 
    else: 
     continue 

Python 3.6 phiên bản của câu trả lời ở trên, sử dụng os - giả định rằng bạn có đường dẫn thư mục như một đối tượng str trong một biến gọi là directory_in_str:

directory = os.fsencode(directory_in_str) 

for file in os.listdir(directory): 
    filename = os.fsdecode(file) 
    if filename.endswith(".asm") or filename.endswith(".py"): 
     # print(os.path.join(directory, filename)) 
     continue 
    else: 
     continue 

Hoặc đệ quy, sử dụng pathlib:

from pathlib import Path 

pathlist = Path(directory_in_str).glob('**/*.asm') 
for path in pathlist: 
    # because path is object not string 
    path_in_str = str(path) 
    # print(path_in_str) 
+1

Điều này dường như chỉ liệt kê các thư mục hoặc tệp ngay trong thư mục. Câu trả lời của pedromateo dưới đây dường như làm một danh sách đệ quy. –

+5

Câu trả lời hay! Tôi có thể thêm rằng bạn cần phải nhập mô-đun "os" Python với: - nhập os – Gabrer

+3

Xin lưu ý rằng trong thư mục Python 3.6 được dự kiến ​​là byte và sau đó listdir sẽ nhổ ra một danh sách tên tệp cũng theo kiểu dữ liệu byte vì vậy bạn không thể chạy endswith trực tiếp trên đó. khối mã này nên được thay đổi cho 'directory = os.fsencode (directory_in_str) for file in os.listdir (thư mục): (" asm") filename = os.fsdecode (file) nếu filename.endswith hoặc filename.endswith (". py"): # print (os.path.join (thư mục, tên tệp)) tiếp tục khác: tiếp tục' –

73

Bạn có thể thử sử dụng glob mô-đun

import glob 

for filename in glob.iglob('/foobar/*.asm'): 
    print('/foobar/%s' % filename) 
56

này sẽ lặp qua tất cả các file hậu duệ, không chỉ là những đứa trẻ trước mắt của các thư mục:

import os 

for subdir, dirs, files in os.walk(rootdir): 
    for file in files: 
     #print os.path.join(subdir, file) 
     filepath = subdir + os.sep + file 

     if filepath.endswith(".asm"): 
      print (filepath) 
+1

Tham chiếu cho hàm os.walk được tìm thấy tại đây: https://docs.python.org/2/library/os.path.html#os.path.walk – ScottMcC

4

Python 3.4 và sau đó cung cấp pathlib trong thư viện chuẩn. Bạn có thể làm:

from pathlib import Path 

asm_pths = [pth for pth in Path.cwd().iterdir() 
      if pth.suffix == '.asm'] 

Hoặc nếu bạn không thích comprehensions danh sách:

asm_paths = [] 
for pth in Path.cwd().iterdir(): 
    if pth.suffix == '.asm': 
     asm_pths.append(pth) 

Path đối tượng có thể dễ dàng được chuyển đổi sang chuỗi.

0

Tôi chưa hoàn toàn hài lòng với việc triển khai này, tôi muốn có một hàm tạo tùy chỉnh thực hiện DirectoryIndex._make(next(os.walk(input_path))) để bạn có thể chuyển đường dẫn mà bạn muốn danh sách tệp. Chỉnh sửa chào mừng!

import collections 
import os 

DirectoryIndex = collections.namedtuple('DirectoryIndex', ['root', 'dirs', 'files']) 

for file_name in DirectoryIndex(*next(os.walk('.'))).files: 
    file_path = os.path.join(path, file_name) 
Các vấn đề liên quan