Tôi đang thử nghiệm một số mã sử dụng os.walk. Tôi muốn tạo một hệ thống tập tin tạm thời, trong bộ nhớ mà tôi có thể cư trú với các tệp mẫu (trống) và các thư mục mà os.walk sẽ trở lại. Điều này sẽ giúp tôi tiết kiệm được sự phức tạp của các cuộc gọi os.walk giả lập để mô phỏng đệ quy.Làm thế nào để giả lập os.walk trong python với một hệ thống tập tin tạm thời?
Cụ thể, mã Tôi muốn thử nghiệm là:
if recursive:
log.debug("Recursively searching for files under %s" % path)
for (dir_path, dirs, files) in os.walk(path):
log.debug("Found %d files in %s: %s" % (len(files), path, files))
for f in [os.path.join(dir_path, f) for f in files
if not re.search(exclude, f)]:
yield f
else:
log.debug("Non-recursively searching for files under %s" % path)
for (dir_path, dirs, files) in os.walk(path):
log.debug("Found %d files in %s: %s" % (len(files), path, files))
for f in [os.path.join(dir_path, f) for f in files
if not re.search(exclude, f)]:
yield f
Đây có phải là có thể trong python?
Bạn có muốn chỉ mô phỏng danh sách được trả về hoặc chức năng giống như tệp không? –
Chỉ các danh sách được trả lại. – jbrown