How to write from a python file to another python script, modify the new python script and save it into a new path.

14 ビュー (過去 30 日間)
replaceLine =2;
fid = fopen('Downloads\mat.py', 'r+');
X = fread(fid);
fclose(fid);
for k=1:(replaceLine-1);
fgetl(X);
end
fseek(X, 1, 'bof');
fprintf(X,'%s',mat_file);
py_File= sprintf('r%d_r%dr.py',pressure_grad,pressure_max); %filename of py file
path1=['F:\excel\' py_file];
fwrite(path1,X);
I am getting an error in fread function and fgetl function.

採用された回答

Akshay Kumar
Akshay Kumar 2018 年 9 月 4 日
編集済み: Akshay Kumar 2018 年 9 月 4 日
The reason for the error in ‘fgetl’ was because you were trying to pass the content 'X' read from the file using 'fread', as input in 'fgetl'.
'fgetl' takes only the file descriptor as input. You can go through these links for more information on fread and fgetl
Additionally, you can go through the below code for reading from one file and writing to another file
fid1 = fopen(readFile, 'r');
line = fgetl(fid1);
lines = cell(0,1);
while(ischar(line))
lines{end+1,1} = line;
line = fgetl(fid1);
end
fclose(fid1);
fid2 = fopen(writeFile, 'a');
for i=1:length(lines)
fprintf(fid2,'%s\n',lines{i});
end
fclose(fid2);
where 'readFile' and 'writeFile' are the variables storing the locations of the files you are trying to read from and write to respectively

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by