我是python的新手,我試圖讀取超過一定大小的文件夾中的所有文件,並將數據(文件路徑和大小)導出到.json在Python中將數據寫入.json文件
我到目前爲止有:
import os
import json
import sys
import io
testPath = str(sys.argv[1])
testSize = int(sys.argv[2])
try:
to_unicode = unicode
except NameError:
to_unicode = str
filesList = []
x = 1
j = "1"
data = {}
for path, subdirs, files in os.walk(testPath):
for name in files:
filesList.append(os.path.join(path, name))
for i in filesList:
fileSize = os.path.getsize(str(i))
if int(fileSize) >= int(testSize):
data['unit'] = 'B'
data['path' + j] = str(i)
data['size' + j] = str(fileSize)
x = x + 1
j = str(x)
with io.open('Files.json', 'w', encoding='utf8') as outfile:
str_ = json.dumps(data,
indent=4, sort_keys=True,
separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))
的問題是,輸出是:
{
"path1": "C:\\Folder\\diager.xml",
"path2": "C:\\Folder\\diag.xml",
"path3": "C:\\Folder\\setup.log",
"path4": "C:\\Folder\\ESD\\log.txt",
"size1": "1908",
"size2": "4071",
"size3": "5822",
"size4": "788",
"unit": "B"
}
但它必須是這樣的:
{
"unit": "B",
"files": [{"path":"C:\Folder\file1.txt", "size": "10"}, {"path":"C:\Folder\file2.bin", "size": "400"}]
}
我添加了j變量中,因爲它只是更換的第一個值,我會剛剛結束了這樣的事情:
{
"path": "C:\\Folder\\diager.xml",
"size": "1908",
"unit": "B"
}
我不知道如何着手...幫助?
取代
data = {"unit": "B", "files": []}
爲了控制字段的順序,可以看到[這個問題](HTTPS:/ /stackoverflow.com/q/10844064/4865672) – gchelfi工程就像一個魅力!此外,我只是將sort_keys設置爲False而不是True,現在不按字母順序打印。謝謝! –