I need to copy a file and change the extension from .seq to mid (without using shell commands)
This works
file = io.open(source_filename, "rb")
source_content = file:read("*all")
file = io.open(source_filename ..".mid", "wb")
file:write(source_content)
file:close()
and I get Song.seq.mid
but I would like Song seq.mid
if I do a
source_filename = string.gsub(source_filename, ".seq", ".mid")
file = io.open(source_filename, "wb")
then file has a nil value file:write(source_content)
You can modify source_filename before opening the file for writing: source_filename = source_filename:gsub("seq$", "mid"). This will replace seq at the end of the filename with mid, achieving the desired effect.
Related
I want to copy elements of an array in a file txt. I read the first 50 samples of a wav file in array a and I want to copy a in a file txt. This is my code:
[s,fs]=wavread('file.wav');
for k=1:50
a=s(k)
end
fid = fopen('file.txt','wt');
fprintf(fid,'%f\n',a);
fclose(fid);
With this code in file txt there is only the last element of a, not all samples.
fprintf can process vector input. So you can simply replace the loop by a = s(1:50);:
[s,fs]=wavread('file.wav');
a = s(1:50);
fid = fopen('file.txt','wt');
fprintf(fid,'%f\n',a);
fclose(fid);
I have several files with datas in it.
For example: file01.csv with x lignes in it, file02.csv with y lines in it.
I would like to treat and merge them with mapreduce in order to get a file with the x lines beginning with file01 then line content, and y files beginning with file02 then line content.
I have two issues here:
I know how to get lines from a file with mapreduce by setting FileInputFormat.setInputPath(job, new Path(inputFile));
But I don't understand how I can get lines of each file of a folder.
Once I have those lines in my mapper, how can I access to the filename corresponding, so that I can create the data I want ?
Thank you for your consideration.
Ambre
You do not need map-reduce in your situation. That's because you want to preserve the order of lines in result file. In this case single thread processing will be faster.
Just run java client with code like this:
FileSystem fs = FileSystem.get();
OutputStream os = fs.create(outputPath); // stream for result file
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
for (String inputFile : inputs) { // reading input files
InputStream is = fs.open(new Path(inputFile));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
}
br.close();
}
pw.close();
I have encountered this problem when reading a JLD file. I have successfully created the file as follows:
using JLD, HDF5
for i in 1:10
file = jldopen("/MY PATH/mydata.jld", "w")
write(file, "A", vector[i] for i in 10 )
close(file)
end
but when I read the file using the following instructions:
file = jldopen("/My PATH/my_tree/mydata.jld", "r")
For this first instruction,it's executed correctly, but when I execute the following:
read(file, "A")
I got this error:
WARNING: type Base.Generator{Core.Int64,##1#2} not present in workspace; reconstructing
ERROR: MethodError: no method matching julia_type(::Void)
in _julia_type(::ASCIIString) at /root/.julia/v0.5/JLD/src/JLD.jl:966
in julia_type(::ASCIIString) at /root/.julia/v0.5/JLD/src/JLD.jl:32
in jldatatype(::JLD.JldFile, ::HDF5.HDF5Datatype) at /root/.julia/v0.5/JLD/src/jld_types.jl:672
in reconstruct_type(::JLD.JldFile, ::HDF5.HDF5Datatype, ::ASCIIString) at /root/.julia/v0.5/JLD/src/jld_types.jl:737
in jldatatype(::JLD.JldFile, ::HDF5.HDF5Datatype) at /root/.julia/v0.5/JLD/src/jld_types.jl:675
in read(::JLD.JldDataset) at /root/.julia/v0.5/JLD/src/JLD.jl:381
in read(::JLD.JldFile, ::ASCIIString) at /root/.julia/v0.5/JLD/src/JLD.jl:357
in eval(::Module, ::Any) at ./boot.jl:237
vector[i] for i in 10 creates a generator, which JLD happily writes to the file for you. You probably want an array, so wrap that expression in collect.
I have this code:
self.y_file = with open("y_file.txt", "r") as g: y_data.append(line for line in g.readlines())
But it doesn't seem to work and I am more than sure the problem lies within 1) How I open the file (with) and that for loop. Any way I could make this work?
you can just open and read. If you want auto-close you need to wrap it in function
self.y_file = open('y_file.txt').readlines()
Or:
def read_file(fname):
with open(fname) as f:
return f.readlines()
self.y_file = read_file('y_file.txt')
I want to read data from a file and save it into an array. Then insert some new data into this array and then save this new data back into the same file deleting what is already there. My code works perfectly, giving me my required data, when I have 'r+' in the fopen parameters, however when I write to the file again it does not delete the data already in the file just appends it to the end as expected. However when I change the permissions to 'w+' instead of 'r+', my code runs but no data is read in or wrote to the file! Anyone know why this might be the case? My code is as seen below.
N = 1021;
b = [0;0;0;0;0];
% Opens file specified by user.
fid = fopen('testing','w+');
% Read header data
Header = fread(fid, 140);
% Move to start of data
fseek(fid,140,'bof');
% Read from end of config header to end of file and save it in an array
% called data
Data = fread(fid,inf);
Data=reshape(Data,N,[]);
b=repmat(b,[1 size(Data,2)]);
r=[b ; Data];
r=r(:);
r = [Header;r];
% write new values into file
fwrite(fid,r);
fclose(fid);
% Opens file specified by user.
fid = fopen('test');
All = fread(fid,inf);
fclose(fid);
According to the documentation, the w+ option allows you to "Open or create new file for reading and writing. Discard existing contents, if any." The contents of the file are discarded, so Data and Header are empty.
You need to set the position indicator of the filehandle before writing. With frewind(fid) you can set it to the beginning of the file, otherwise the file is written / appended at the current position.
N = 1021;
b = [0;0;0;0;0];
% Opens file specified by user.
fid = fopen('testing','r+');
% Read header data
Header = fread(fid, 140);
% Move to start of data
fseek(fid,140,'bof');
% Read from end of config header to end of file and save it in an array
% called data
Data = fread(fid,inf);
Data=reshape(Data,N,[]);
b=repmat(b,[1 size(Data,2)]);
r=[b ; Data];
r=r(:);
r = [Header;r];
% write new values into file
frewind(fid);
fwrite(fid,r);
fclose(fid);
% Opens file specified by user.
fid = fopen('test');
All = fread(fid,inf);
fclose(fid);