Below code scans the folders and list all the files older than threshold date. For instance of the threshold time is now-12hrs then it will list all the files older than threshold and save them in variable.
string_output = ""
my_paths = []
for p in ['\\\location\\folderpath\\subfolder\\']:
for dirpath, dirnames, filenames in os.walk(p):
my_paths.append(filenames)
for filename in filenames:
full_path = os.path.join(dirpath, filename)
if last_modified(full_path) < threshold:
string_output+= '\n{}\n{}\n{}\nFile Name: {}\nFile Size: {}\nFile Created: {}\nFile Last Modified: {}\n{}'.format(
separator, timestamp, app_name, full_path,
file_size(full_path),
created(full_path),
last_modified(full_path),separator)
print string_output;
string format output looks good without extra backslah
----------------------------------------
09/06/2017 13:10:07
File Name: \\location\folderpath\subfolder\1.txt
File Size: 153.0 bytes
File Created: 2017-01-26 14:29:59
File Last Modified: 2017-01-26 14:28:39
----------------------------------------
09/06/2017 13:10:07
File Name: \\location\folderpath\subfolder\2.txt
File Size: 153.0 bytes
File Created: 2017-01-26 14:29:59
File Last Modified: 2017-01-26 14:28:39
But when I replaced the string.format with json dumps the looping doesn't work
string_output = ""
my_paths = []
for p in ['\\\location\\folderpath\\subfolder\\']:
for dirpath, dirnames, filenames in os.walk(p):
my_paths.append(filenames)
for filename in filenames:
full_path = os.path.join(dirpath, filename)
if last_modified(full_path) < threshold:
string_output=json.dumps({'Timestamp: ':timestamp, 'FileName : ':full_path, 'File Size : ':file_size(full_path), 'File Created : ':created(full_path),'File Last Modified : ':last_modified(full_path) }, sort_keys=False, indent=8)
print string_output;
Json output with extra backslash (same as input) and only one record
{
"FileName : ": "\\\location\\folderpath\\subfolder\2.txt",
"Timestamp: ": "Timestamp: 09/06/2017 13:10:07",
"File Created : ": "2017-01-26 14:29:59",
"File Last Modified : ": "2017-01-26 13:14:11",
"File Size : ": "48.0 bytes"
}
How to make json.dumps to list all the files instead of 1 result and output without extra backslash?
string_output
using+=
. In the second example, you're assigning using=
so you're replacing whatever used to be there...so of course you only see a single record of output. – larsks Sep 6 '17 at 18:24