Skip to content

Instantly share code, notes, and snippets.

@arischow
Created October 21, 2016 02:24
Show Gist options
  • Save arischow/a08b1f2701e15d0ce2c87d6e53a2fd82 to your computer and use it in GitHub Desktop.
Save arischow/a08b1f2701e15d0ce2c87d6e53a2fd82 to your computer and use it in GitHub Desktop.
获取某目录下的所有同类型文件
# 获取某目录下的所有同类型文件
import os
import fnmatch
matches = []
for root, dirs, files in os.walk('/Users/arischow/Desktop/data'):
for file in fnmatch.filter(files, '*.txt'):
matches.append(os.path.join(root, file))
print(matches)
for match in matches:
with open(match, 'r', encoding='utf8') as original_file:
with open('/Users/arischow/Desktop/data/numberOnly.txt', 'a') as new_file:
for line in original_file:
line = line.split(',')[-1]
new_file.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment