I want to get the filename of the latest file in a directory. latest is based on creation time.
Currently I stuck in sorting the 2-dimensional array. I do not know how I should sort it? I get the following error
ERROR: LoadError: MethodError: no method matching isless(::Array{Any,1}, ::Array{Any,1})
The 2-dimensional array looks like this:
Any[
Any[1.47913e9,"foo.csv"],
Any[1.47913e9,"bar.csv"],
Any[1.47913e9,"foobar.csv"]
]
newestfile.jl
dfolder = "C:\\Users\\Foo\\Downloads"
cd( dfolder )
dfiles = readdir( "." )
files=[]
#println( dfiles )
for file in dfiles
created = ctime( file )
push!(files, [created, file] )
end
println( files )
# sort the timestamp
sort!( files ) # This throws an error
# grab the newst file and display the filename
How do I display the newest file in the directory?
Try:
julia> sort!( files, by = e -> e[1])
The last item is the newest one:
julia> files[end]
2-element Array{Any,1}:
1.48061e9 ".whatever"
The filename is:
julia> files[end][2]
".whatever"
Related
I am unable to parse a JSON array from a text file due to errors and my limited knowledge of JSON.
The file looks something like this [{"random":"fdjsf","random56":128,"name":"dsfjsd", "rid":1243,"rand":674,"name":"dsfjsd","random43":722, "rid":126},{"random":"fdfgfgjsf","random506":120,"name":"dsfjcvcsd", "rid":12403,"rando":670,"name":"dsfooojsd","random4003":720, "rid":120}] It has more than one object({}) in the entire array however I did not want to include all 600. The layout shown above is basically how all of them look.
r = s.get(getAPI, headers=header, verify=False)
f = open('text.txt', 'w+')
f.write(r.text)
f.close
output_file = open ('text.txt', 'r')
json_array = json.load(output_file)
json_list = []
for item in json_array:
name = "name"
rid = "rid"
json_items = {name:None, rid:None}
json_items = [name] = item[name]
json_items = [rid] = item[rid]
json_list.append(json_items)
print(json_list)
I would like to loop through an array and find any time it says "name":... eventually followed by "rid":... and store those in a dictionary as key value pairs.
Errors:
ValueError: too many values to unpack (expected 1)
There is a syntax error when you assign values to json_items, change it to:
json_items[name] = item[name]
json_items[rid] = item[rid]
i'm trying to follow this tutorial with my own local data files:
CNTK tutorial
i have the following function to save my data array into a txt file feedable to CNTK:
# Save the data files into a format compatible with CNTK text reader
def savetxt(filename, ndarray):
dir = os.path.dirname(filename)
if not os.path.exists(dir):
os.makedirs(dir)
if not os.path.isfile(filename):
print("Saving", filename )
with open(filename, 'w') as f:
labels = list(map(' '.join, np.eye(11, dtype=np.uint).astype(str)))
for row in ndarray:
row_str = row.astype(str)
label_str = labels[row[-1]]
feature_str = ' '.join(row_str[:-1])
f.write('|labels {} |features {}\n'.format(label_str, feature_str))
else:
print("File already exists", filename)
i have 2 ndarrays of the following shape that i want to feed the model:
train.shape
(1976L, 15104L)
test.shape
(1976L, 15104L)
Then i try to implement the fucntion like this:
# Save the train and test files (prefer our default path for the data)
data_dir = os.path.join("C:/Users", 'myself', "OneDrive", "IA Project", 'data', 'train')
if not os.path.exists(data_dir):
data_dir = os.path.join("data", "IA Project")
print ('Writing train text file...')
savetxt(os.path.join(data_dir, "Train-128x118_cntk_text.txt"), train)
print ('Writing test text file...')
savetxt(os.path.join(data_dir, "Test-128x118_cntk_text.txt"), test)
print('Done')
and then i get the following error:
Writing train text file...
Saving C:/Users\A702628\OneDrive - Atos\Microsoft Capstone IA\Capstone data\train\Train-128x118_cntk_text.txt
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-24-b53d3c69b8d2> in <module>()
6
7 print ('Writing train text file...')
----> 8 savetxt(os.path.join(data_dir, "Train-128x118_cntk_text.txt"), train)
9
10 print ('Writing test text file...')
<ipython-input-23-610c077db694> in savetxt(filename, ndarray)
12 for row in ndarray:
13 row_str = row.astype(str)
---> 14 label_str = labels[row[-1]]
15 feature_str = ' '.join(row_str[:-1])
16 f.write('|labels {} |features {}\n'.format(label_str, feature_str))
IndexError: list index out of range
Can somebody please tell me what's going wrong with this part of the code? And how could i fix it? Thank you very much in advance.
Since you're using your own input data -- are they labelled in the range 0 to 9? The labels array only has 10 entries in it, so that could cause an out-of-range problem.
I have that code,
h1 = dir('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)'); %angry
h2 = dir('C:\Users\John\Documents\MATLAB\code for yannis\neutral(N)');%neutral
h3 = dir('C:\Users\John\Documents\MATLAB\code for yannis\happiness(F)');%happy
%fprintf('%s', filename);%filename h(i,1).name
fprintf('%d \n',numel(h1));
fprintf('%d \n',numel(h2));
fprintf('%d \n',numel(h3));
%fprintf('%d', max(numel(h1),numel(h2),numel(h3)));
A= [numel(h1) numel(h2) numel(h3)];
fprintf('%d \n \n \n', max(A));
fprintf('%s \n', h1(2).name);
%load('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\*.mat');
fprintf('%d \n \n \n', length(h1));
resultsdir = 'results';
addpath('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\');
array = [h1(2)];
for i=3:max(A)+3
%s= 'C:\Users\John\Documents\MATLAB\code for yannis\anger(W)';
thisfile = h1(i).name;
destfile = fullfile(resultsdir, thisfile);
thisdata = load(thisfile);
%array[thisdata];
%strcmp(h(i,1).name(1:2), s);
%cat(1, array, h1(i,1).M);
fprintf('%s \n', h1(i,1).name);
end
and i want to keep all the .mat files in an array in order to compare it with other mat files
Thanks in advance
You can select the top level folder and utilize your system's dir call to recursively search for all files that match your criteria (*.mat, in this case)
For example:
mypath = uigetdir('', 'Select Top Level Folder');
oldpath = cd(mypath); % cd to data directory for simpler dir call
[~, cmdout] = system('dir /S /B *.mat');
cd(oldpath); % Return to previous path
mymatfiles = regexp(cmdout, '(.:\\[\w\-\\. ]+\.\w+)', 'match');
The system call returns one long string with all of the absolute paths to your *.mat files. I utilize a regexp to split this into a cell array, where each cell is an absolute path to a single *.mat file.
Note that this is Windows only because it uses the MS-DOS dir command. This function can easily be adapted to other operating systems, I just don't know them.
Assuming you have .mat files in all of those directories (ending in ".mat"), simply get the directory listings of each of those directories and store them.
% All of the folders where the *.mat files live
folders = {'C:\folder1'; 'C:\folder2'};
allfiles = cell();
for k = 1:numel(folders)
% Find all the .mat files
files = dir(fullfile(folders{k}, '*.mat'));
% Convert to absolute paths
fullpaths = cellfun(#(x)fullfile(folders{k}, x), {files.name}, 'uniform', 0);
% Store in our global cell array
allfiles = cat(1, allfiles, fullpaths(:));
end
Now allfiles contains an entry for every .mat file contained within those paths. You can then loop through this cell array to perform any operations you need.
alldata = cellfun(#load, allfiles, 'uniform', 0);
I want to use an expression like
#{ %$hashref{'key_name'}[1]
or
%$hashref{'key_name}->[1]
to get - and then test - the second (index = 1) member of an array (reference) held by my hash as its "key_name" 's value. But, I can not.
This code here is correct (it works), but I would have liked to combine the two lines that I have marked into one single, efficient, perl-elegant line.
foreach my $tag ('doit', 'source', 'dest' ) {
my $exists = exists( $$thisSectionConfig{$tag});
my #tempA = %$thisSectionConfig{$tag} ; #this line
my $non0len = (#tempA[1] =~ /\w+/ ); # and this line
if ( !$exists || !$non0len) {
print STDERR "No complete \"$tag\" ... etc ... \n";
# program exit ...
}
I know you (the general 'you') can elegantly combine these two lines. Could someone tell me how I could do this?
This code it testing a section of a config file that has been read into a $thisSectionConfig reference-to-a-hash by Config::Simple. Each config file key=value pair then is (I looked with datadumper) held as a two-member array: [0] is the key, [1] is the value. The $tag 's are configuration settings that must be present in the config file sections being processed by this code snippet.
Thank you for any help.
You should read about Arrow operator(->). I guess you want something like this:
foreach my $tag ('doit', 'source', 'dest') {
if(exists $thisSectionConfig -> {$tag}){
my $non0len = ($thisSectionConfig -> {$tag} -> [1] =~ /(\w+)/) ;
}
else {
print STDERR "No complete \"$tag\" ... etc ... \n";
# program exit ...
}
i have many data.frames() that i am trying to send to MySQL database via RMySQL().
# Sends data frame to database without a problem
dbWriteTable(con3, name="SPY", value=SPY , append=T)
# stock1 contains a character vector of stock names...
stock1 <- c("SPY.A")
But when I try to loop it:
i= 1
while(i <= length(stock1)){
# converts "SPY.A" into SPY
name <- print(paste0(str_sub(stock1, start = 1, end = -3))[i], quote=F)
# sends data.frame to database
dbWriteTable(con3,paste0(str_sub(stock1, start = 1, end = -3))[i], value=name, append=T)
i <- 1+i
}
The following warning is returned & nothing was sent to database
In addition: Warning message:
In file(fn, open = "r") :
cannot open file './SPY': No such file or directory
However, I believe that the problem is with pasting value onto dbWriteTable() since writing dbWriteTable(con3, "SPY", SPY, append=T) works but dbWriteTable(con3, "SPY", name, append=T) will not...
You are probably using a non-base package for str_sub and I'm guessing you get the same behavior with substr. Does this succeed?
dbWriteTable(con3, substr( stock1, 1,3) , get(stock1), append=T)