工作試試這個:在輸出文件
import os
import re
import sys
SEPARATOR = ';'
CSV_FILE_PATH = 'output.csv'
if __name__ == "__main__":
if len(sys.argv) != 2:
print "usage: create_csv <base_path>"
sys.exit(1)
path = sys.argv[1]
# only folders in specified folder
folders = filter(os.path.isdir, os.listdir(path))
with open(CSV_FILE_PATH, 'w') as csv_file:
for folder in folders:
# folder path
folder_path = os.path.join(path, folder)
# extracting number
match = re.match(r's(\d+)', folder) # s\d+ is the pattern of your folder name, for example s20
if match is not None:
number = match.group(1)
# loop for files within the folder
for filename in os.listdir(folder_path):
# full file path
filepath = os.path.join(folder_path, filename)
# writing result
line = SEPARATOR.join((filepath, number))
csv_file.write(line + '\n')
沒有,先生。讓我們看看預期的輸出文件。假設比在文件夾包含s14,s20 ....而我只調用python.exe make_folder.py在 – Jame 2015-04-02 10:05:47