Write output of replaceStream to new file - node.js

I am writing a progrma in node and using replaceStream and I want to write the out of replacestream to a file, but all I can get it to do write now is print to the console using this code:
fs.createReadStream(filePath + "/" + fileName)
.pipe(replaceStream(imagePath, newPath))
.pipe(process.stdout);

Just write it to a writable stream.
fs.createReadStream(filePath + "/" + fileName)
.pipe(replaceStream(imagePath, newPath))
.pipe(fs.createWriteStream(filePath + "/" + newFileName));

Related

Escape Backslash with String Combination

I am working on a program that clicks a button when it changes color, and at the end of the program, it screenshots the webpage, saves the screenshot, and then moves it to a different directory. Everything seems to be working fine except for moving the file into a different folder. The code I am using to move the file is here:
os.replace("'\\'" + fileName, "'\\'" + saveName + "'\\'" + fileName)
I get the error:
FileNotFoundError: [WinError 3] The system cannot find the path specified: "'\'0.png" -> "'\'saves216'\'0.png"
I don't know how to get the backslash to escape without becoming a double backslash
Remove the extra quotes:
os.replace("\\" + fileName, "\\" + saveName + "\\" + fileName)
You directly escape a \ with another one:
>>> s = "\\" + "filename"
>>> print(s)
\filename
os.replace("\\" + fileName, "\\" + saveName + "\\" + fileName)
extra '' in yours
change the filename variable too if it has ''

download an image file and add it to Put Request

I need to download a file from an url and use a put request to upload it somewhere else
Download is done with
r=requests.get(image_url, auth=HTTPBasicAuth( user , password))
header_content_type = r.headers.get('content-type')
fileType = header_content_type.split('/')[-1]
content_type = header_content_type.split(';')[-1]
file_extension = fileType.split(';',1)[0]
file_name = file_id+'.' + file_extension
open('downloads/' + file_name , 'wb').write(r.content)
which works fine and stores the file locally in the downloads folder.
I can open the image with any image viewer and it works fine.
the put request needs to look like
{ "data":"gsddfgdsfg...(base64) ", "filename":"example2.txt", "contentType":"plain/text" }
I have tried to do it like following
def build_step_attachment_json(path, filename, contentype):
with open(path+filename) as f:
encoded = base64.b64encode(f.read())
return '{ "data":"'+ encoded + '", "filename":"' + filename +'", "contentType":" '+ contentype + '" }'
but it fails with:
"UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 44: character maps to "

image processing commands in python using for loop

I have this part code which i don't understand the for loop part:
also what does file path holds and how it is different than all file path?
all_files_path = glob.glob("ADNI1_Screening_1.5T/ADNI/*/*/*/*/*.nii")
for file_path in all_file_path:
print(os.system("./runROBEX.sh " + file_path + " stripped/" +file_path.split("/")[-1]))

renameTo() in grails to rename an apk file to zip file

I want to upload apk files. And then convert it to zip in order to extract some information.
I used renameTo() function but its not working.
String newFilename = "new.apk"
new File(f.getOriginalFilename()).renameTo(new File("new.apk"))
(f.getOriginalFilename() will return the name of an apk file) How exactly can I do renaming?
Got it. need to give the full path to the file. for example
new File( webRootDir + "/" + [folder name] + "/" +f.originalFilename ).renameTo(
new File(webRootDir + "/" + [folder name] + "/" +appname+".zip") )
will solve the problem.
And Groovier using GStrings:
new File("$webRootDir/$folderName/${f.originalFilename}").renameTo(new File("$webRootDir/$folderName/$appname.zip"))

Why is File.separator using the wrong character?

I'm trying to add functionality to a large piece of code and am having a strange problem with File Separators. When reading a file in the following code works on my PC, but fails when on a Linux server. When on PC I pass this and it works:
fileName = "C:\\Test\\Test.txt";
But when on a server I pass this and get "File Not Found" because the BufferedReader/FileReader statement below swaps "/" for "\":
fileName = "/opt/Test/Test.txt";
System.out.println("fileName: "+fileName);
reader = new BufferedReader(new FileReader(new File(fileName)));
Produces this output when run on the LINUX server:
fileName: /opt/Test/Test.txt
File Not Found: java.io.FileNotFoundException: \opt\Test\Test.txt (The system cannot find the path specified)
When I create a simple Test.java file to try and replicate it behaves as expected, so something in the larger code source is causing the BufferedReader/FileReader line to behave as if it's on a PC, not a Linux box. Any ideas what that could be?
I don't see where you used File.separator. Try this instead of hard coding the path separators.
fileName = File.separator + "opt" + File.separator + "Test" + File.separator + "Test.txt";

Resources